Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何对UIImage或UIImageview进行像素化和取消像素化?_Ios_Swift_Uiimageview_Uiimage - Fatal编程技术网

Ios 如何对UIImage或UIImageview进行像素化和取消像素化?

Ios 如何对UIImage或UIImageview进行像素化和取消像素化?,ios,swift,uiimageview,uiimage,Ios,Swift,Uiimageview,Uiimage,我想使用Swift对UIImage或UIImageview进行像素化和取消像素化,但我不知道如何才能做到这一点 也许使用特效,图层或者类似的东西 这在iOS上是一项非常简单的任务 像素化 您可以使用核心映像 结果 默认的inputScale值是8。但是,您可以通过手动设置参数来增加或减少效果 filter.setValue(8, forKey: "inputScale") // ^ // change this 延伸 您还可以定义以下扩展 exten

我想使用Swift对
UIImage
UIImageview
进行像素化和取消像素化,但我不知道如何才能做到这一点


也许使用特效,图层或者类似的东西

这在iOS上是一项非常简单的任务

像素化 您可以使用核心映像

结果

默认的
inputScale
值是
8
。但是,您可以通过手动设置参数来增加或减少效果

filter.setValue(8, forKey: "inputScale")
//              ^
//         change this
延伸 您还可以定义以下扩展

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)
    }
}

脱钩 机制完全相同,只需使用不同的过滤器。您可以找到过滤器的完整列表(但请查看每个过滤器可用/需要哪些参数)。我认为他们能胜任这项工作

当然,不要期望能够输入低分辨率的超像素图像,并获得高清晰度图像。此技术仅在X-Files:D中可用



蘑菇图像已从中拍摄。

非常感谢!!祝你今天愉快@appzYourLife感谢您提供了这本很棒的教程。您知道如何在不将UIView转换为UIImage的情况下将CIFilter应用于常规UIView吗?非对称大白圈正常吗?@Jaume我想是的。原始图像中的圆具有不对称阴影。
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)
    }
}