Correct crop of CIGaussianBlur

后端 未结 9 1601
北海茫月
北海茫月 2020-11-29 22:09

As I noticed when CIGaussianBlur is applied to image, image\'s corners gets blurred so that it looks like being smaller than original. So I figured out that I need to crop i

9条回答
  •  庸人自扰
    2020-11-29 22:12

    I saw some of the solutions and wanted to recommend a more modern one, based off some of the ideas shared here:

    private lazy var coreImageContext = CIContext() // Re-use this.
    
    func blurredImage(image: CIImage, radius: CGFloat) -> CGImage? {
        let blurredImage = image
            .clampedToExtent()
            .applyingFilter(
                "CIGaussianBlur",
                parameters: [
                    kCIInputRadiusKey: radius,
                ]
            )
            .cropped(to: image.extent)
    
        return coreImageContext.createCGImage(blurredImage, from: blurredImage.extent)
    }
    

    If you need a UIImage afterward, you can of course get it like so:

    let image = UIImage(cgImage: cgImage)
    

    ... For those wondering, the reason for returning a CGImage is (as noted in the Apple documentation):

    Due to Core Image's coordinate system mismatch with UIKit, this filtering approach may yield unexpected results when displayed in a UIImageView with "contentMode". Be sure to back it with a CGImage so that it handles contentMode properly.

    If you need a CIImage you could return that, but in this case if you're displaying the image, you'd probably want to be careful.

提交回复
热议问题