How to pixelate and secure UIImage or UIImageview?

I would like to pixelize and unpixelize UIImageeither UIImageviewusing Swift, but I don't know how to do this.

Is it possible to use effects, a layer or something like that?

+4
source share
1 answer

This is a very simple task for iOS.

pixelation

You can use the CIPixellate Basic Image Filter .

func pixellated(image: UIImage) -> UIImage? {
    guard let
        ciImage = CIImage(image: image),
        filter = CIFilter(name: "CIPixellate") else { return nil }
    filter.setValue(ciImage, forKey: "inputImage")
    guard let output = filter.outputImage else { return nil }
    return UIImage(CIImage: output)
}

Result

enter image description here

The default value inputScale 8. However, you can increase or decrease the effect manually by setting the parameter.

filter.setValue(8, forKey: "inputScale")
//              ^
//         change this

Extension

You can also define the following extension

extension UIImage {
    func pixellated(scale: Int = 8) -> UIImage? {
        guard let
            ciImage = UIKit.CIImage(image: self),
            filter = CIFilter(name: "CIPixellate") else { return nil }
        filter.setValue(ciImage, forKey: "inputImage")
        filter.setValue(scale, forKey: "inputScale")
        guard let output = filter.outputImage else { return nil }
        return UIImage(CIImage: output)
    }
}

enter image description here

Unpixelation

, . ( , / ). , CIGaussianBlur .

, , . X-Files: D


.

+10

Source: https://habr.com/ru/post/1618703/


All Articles