Search code examples
iosswiftuiimageviewuiimagecifilter

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.


Solution

  • 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