简体   繁体   中英

Useing CIFilter to create image cause rendering very slow on iOS

I use CIFilter to create a UIImage then add it on a UIImageView. The process of creating this image is really fast and I can add it on the image view. But the whole UI is freeze for a few seconds until it shows the filtered image. I checked that the CIFilter call is fast. I think the slow is caused by UIImageView to render the image. Why is it such slow if the image already created? Below is the code to make a filter of a image.

func photoEffectChrome() -> CIFilter {
    let filter = CIFilter(name: "CIPhotoEffectChrome")!
    return (filter)
}

func outputImage(filter: CIFilter, originalImage: UIImage) -> UIImage{
    print(filter)
    let inputImage = CIImage(image: originalImage)
    filter.setValue(inputImage, forKey: kCIInputImageKey)
    let cgImage = context!.createCGImage(filter.outputImage!, fromRect: (filter.outputImage?.extent)!)
    return UIImage(CGImage: cgImage, scale: 1, orientation: originalImage.imageOrientation)
}

The call on the above methods happens on a background thread, then I use blow method to add it on a scroll view.

dispatch_async(dispatch_get_main_queue(), {
            self.filterScrollView.addSubview(uiView)
        })

If I comment out the "self.filterScrollView.addSubview(uiView)", the UI runs smoothly. Why does it take a long time for rendering the image? More specifically, it happens on simulator. It works much faster when running on a device.

But the whole UI is freeze for a few seconds until it shows the filtered image

A problem of this sort suggests that the method in which you set the UIImageView's image (not shown in your question) is being called on a background thread. You mustn't do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM