LoginSignup
3
4

More than 5 years have passed since last update.

Swift 画像フィルタ処理後のCIImageをUIImageへ変換後の問題

Last updated at Posted at 2018-01-28

写真を撮影後生成したUIImageデータを各種フィルター処理により生成されたCIImageデータをそのまま
UIImageに戻した場合その後の画像処理においてエラーが発生する場合の対策。
下記は画像をシャープ処理をするフィルター処理後生成されたCIImageを下記のように一旦CGImageに
変換後さらにUIImageに戻すことによりエラーを回避できる。

    func filter01(image: UIImage) -> UIImage?{
        let ciImage:CIImage = CIImage(image:image)!
        // カラーエフェクトを指定してCIFilterをインスタンス化.
        let mySharpFilter = CIFilter(name:"CIUnsharpMask")

        // イメージのセット.
        let radius = 8
        mySharpFilter!.setValue(ciImage, forKey: kCIInputImageKey)
        mySharpFilter!.setValue(radius, forKey: "inputRadius")
        mySharpFilter!.setValue(NSNumber(float: 1.0), forKey: "inputIntensity")
        print("ok4")

        // フィルターを通した画像をアウトプット. 
        //修正前let image2 = UIImage(CIImage: mySharpFilter!.outputImage!)
        let context = CIContext()
        let cgImage = context.createCGImage(mySharpFilter!.outputImage!,fromRect: mySharpFilter!.outputImage!.extent)
        let image2 = UIImage(CGImage: cgImage)
        return image2
    }
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4