Kazuki Tatai
Kazuki Tatai

Reputation: 63

UIImageVIew crash when CIFiltered UIImage setted

I'm trying to "CIFiltering" some images. But UIImageView crash when putting filtered uiimage. I've never seen this situation and don't know next step to how figure out. Please help or teach some tips. Thank you.

https://developer.apple.com/documentation/coreimage/simulating_scratchy_analog_film

enter image description here This is what happens.

Upvotes: 0

Views: 124

Answers (1)

Sharad Chauhan
Sharad Chauhan

Reputation: 4891

Please do the following changes in the function :

At the beginning of the function add :

public func filter(_image) {
    let aUIImage     = _image

    guard let aCGImage = aUIImage.cgImage else { return }
    let aCIImage       = CIImage(cgImage: aCGImage)
    let context        = CIContext(options: nil)

and replace this line let image = UIImage(ciImage: speckledImage) with :

let imageRef = context.createCGImage(speckledImage, from: aCIImage.extent)
let image    = UIImage(cgImage: imageRef!)

Reason :

You are creating an CIImage and trying to attach it to UIImageView which is not possible. According to apple docs : Although a CIImage object has image data associated with it, it is not an image.

Refer this link for more information : https://developer.apple.com/documentation/coreimage/ciimage

Upvotes: 2

Related Questions