2
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

[Swit5]CIFilterを試してみた

CIFilterを公式ドキュメントのコードを参考にして、画像の色を変更してみました。
Core Image Filter Referenceを見ると、色々と出来そうだと感じました。
今後、他にも試してみたいと思います。

環境

Xcode12.3
Swift5.3.2
macOS Big Sur 11.2.3

元の画像

スクリーンショット 2021-05-06 17.38.57.png

画像をセピア色に変更

スクリーンショット 2021-05-06 18.06.43.png

セピア色にした時の全コード

import UIKit

class ViewController: UIViewController {

    let image = UIImage(named: "sydney.jpg")

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let originalCIImage = CIImage(image: image!)
        let sepiaCIImage = sepiaFilter(originalCIImage!, intensity: 0.9)
        self.imageView.image = UIImage(ciImage: sepiaCIImage!)

    }

    func sepiaFilter(_ input: CIImage, intensity: Double) -> CIImage? {

        let sepiaFilter = CIFilter(name: "CISepiaTone")
        sepiaFilter?.setValue(input, forKey: kCIInputImageKey)
        sepiaFilter?.setValue(intensity, forKey: kCIInputIntensityKey)
        return sepiaFilter?.outputImage
    }
}

参照サイト

https://developer.apple.com/documentation/coreimage/processing_an_image_using_built-in_filters
https://developer.apple.com/documentation/coreimage/cifilter
https://qiita.com/shtnkgm/items/fb1425ef88d31fdad1d4
https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIAccordionFoldTransition

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
2
Help us understand the problem. What are the problem?