ios: how to blur a image with CGPath?

孤街浪徒 提交于 2019-12-06 04:03:23

问题


I create a CGPath area as shown green circle. The CGPath area need to be clear, and the rest of image will be applied blurry or translucent effect, I can clip image inside CGPath with following code:

UIGraphicsBeginImageContext(view.frame.size);
CGContextAddPath(ctx, path);
CGContextClip(ctx);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(clipImage, nil, nil, nil);
CGPathRelease(path);

but I don't know how to apply blurry or translucent effect with CGPath simultaneously. I think I can blur origin image and merge it with clip image, but I don't know how to implement it.


回答1:


You required 2 concept to achieved end result :-

i) CGBlendMode ii) CIFilter

CGBlendMode is Used to remove the desired portion from image. Compositing operations on paths residing one another in the Current Context . Clear mode whose rawValue is 16 help to clear the desired portion.


CIFilter help to blur the final image.

class ConvertToBlurryImage:UIView {

    var originalImage:UIImage!
    var finalImage:UIImage!

    override func draw(_ rect: CGRect) {
        super.draw(rect)


        //Original Image
        originalImage = UIImage(named: "originalImage.png")

        //Intermediate Image
        let intermediateImage = UIImage().returnBlurImage(image: originalImage)

        //Final Result Image
        finalImage =  blendImage(image: intermediateImage)

        let attachedImage = UIImageView(image: finalImage)
        addSubview(attachedImage)

    }

    func blurryImage(image:UIImage) -> UIImage {

        UIGraphicsBeginImageContext(frame.size)
        image.draw(in: CGRect(origin: frame.origin, size: frame.size) )

         //  16 === clear
        let mode = CGBlendMode(rawValue: 16)
        UIGraphicsGetCurrentContext()!.setBlendMode(mode!)

        //Path that need to crop
        pathToCrop()

        let mode2 = CGBlendMode(rawValue: 16)
        UIGraphicsGetCurrentContext()!.setBlendMode(mode2!)

        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        return finalImage!

    }

    func pathToCrop() {
        let path = UIBezierPath(ovalIn: CGRect(x: frame.width/2 - 50, y: frame.height/2 - 100 , width: 150, height: 150) )
        path.fill()
        path.stroke()
    }


}

extension UIImage {

    func returnBlurImage(image:UIImage) -> UIImage {

        let beginImage = CIImage(cgImage: image.cgImage!)
        let blurfilter = CIFilter(name: "CIGaussianBlur")
        blurfilter?.setValue(beginImage, forKey: "inputImage")
        let resultImage = blurfilter?.value(forKey: "outputImage") as! CIImage
        let blurredImage = UIImage(ciImage: resultImage)

        return blurredImage



    }

}

Mission Achived

Final Github Demo



来源:https://stackoverflow.com/questions/16803763/ios-how-to-blur-a-image-with-cgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!