简体   繁体   中英

How to use CIFilter sunbeamsGenerator in Swift 5 and iOS 13?

Please, could someone to provide me an example code for CIFilter sunbeamsGenerator in Swift 5 and iOS 13?

I am trying to create an extension to apply this filter and I have this error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inputImage.'

The code works ok for the Blur filter but not for the sunbeamsGenerator

import UIKit
import CoreImage.CIFilterBuiltins

public extension UIImage {

public func withBlurFilter() -> UIImage {
       let ciImage = CIImage(image: self)!
       let filter = CIFilter.gaussianBlur()
       filter.inputImage = ciImage
       filter.radius = 30
       let vintage = ciImage.applyingFilter(filter.name)
       return vintage.uiImage()
   }

    public func withSunFilter() -> UIImage {
        let ciImage = CIImage(image: self)!
        let filter = CIFilter.sunbeamsGenerator()
        let sunbeam = ciImage.applyingFilter(filter.name)
        return sunbeam.uiImage()
    }

}

Thanks on advance, I am new in CoreImage

The CISunbeamsGenerator doesn't need an input image – it will generate a sunbeam effect (hence the name) that you can use to, for instance, blend over another image.

When you call ciImage.applyingFilter(...) it will try to assign the image as inputImage to the given filter. But generators don't have that property, hence the error.

You can do something like this instead:

public func withSunFilter() -> UIImage {
    let ciImage = CIImage(image: self)!
    let filter = CIFilter.sunbeamsGenerator()
    let sunbeam = filter.outputImage!
    let output = sunbeam.composited(over: ciImage)
    return output.uiImage
}

Check the parameters of CISumbeamsGenerator to see what you can do with it.

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