Xcode9学习笔记57 - 使用CoreImage框架给图片添加马赛克效果 CIPixellate
import UIKit import CoreImage class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let image = UIImage(named: "Pic1") let imageView = UIImageView(image: image) self.view.addSubview(imageView) let ciImage = CIImage(image: image!)//初始化一个图像对象并加载之前导入的图片 let filter = CIFilter(name: "CIPixellate")//初始化一个滤镜对象,并设置滤镜类型为像素化滤镜 filter?.setDefaults()//设置像素画滤镜采用默认的配置选项 filter?.setValue(ciImage, forKey: kCIInputImageKey)//设置需要应用像素化滤镜的图像 let outImamge = filter?.outputImage//获得应用像素化滤镜后的图像 imageView.image = UIImage(ciImage: outImamge!)//更改图像视图的内容为应用滤镜后的图像 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }