zoukankan      html  css  js  c++  java
  • [Xcode 实际操作]六、媒体与动画-(5)使用CoreImage框架给图片添加马赛克效果

    目录:[Swift]Xcode实际操作

    本文将演示如何使用CoreImage图像处理框架,给图片添加像素化的滤镜效果。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

     1 import UIKit
     2 //首先导入要使用的框架,该框架提供了强大和高效的图像处理功能,
     3 //用来对基于像素的图像进行分析、操作和特效处理
     4 import CoreImage
     5 
     6 class ViewController: UIViewController {
     7 
     8     override func viewDidLoad() {
     9         super.viewDidLoad()
    10         // Do any additional setup after loading the view, typically from a nib.
    11         
    12         //从项目资源文件中读取一张图片
    13         let image = UIImage(named: "Picture")
    14         //创建一个图像视图对象,
    15         //并给图像视图指定需要显示的图片
    16         let imageView = UIImageView(image: image)
    17         //将图像视图,添加到当时视图控制器的根视图
    18         self.view.addSubview(imageView)
    19         
    20         //然后初始化一个CoreImage图像对象,并加载之前导入的图片
    21         let ciImage = CIImage(image: image!)
    22         //初始化一个滤镜对象,并设置滤镜类型为像素化滤镜
    23         let filter = CIFilter(name: "CIPixellate")
    24         //设置像素化滤镜,采用默认的配置选项
    25         filter?.setDefaults()
    26         //设置需要应用该滤镜的图像
    27         filter?.setValue(ciImage, forKey: kCIInputImageKey)
    28         //接着获得应用指定滤镜之后的图像
    29         let outImage = filter?.outputImage
    30         
    31         //更改图像视图的内容,为应用滤镜后的图像。
    32         imageView.image = UIImage(ciImage: outImage!)
    33     }
    34 
    35     override func didReceiveMemoryWarning() {
    36         super.didReceiveMemoryWarning()
    37         // Dispose of any resources that can be recreated.
    38     }
    39 }
  • 相关阅读:
    SpringSecurity 框架学习 3
    SpringSecurity 框架学习 项目创建
    nginx 限制ip访问
    nginx 负载均衡,后端服务获取不到域名问题
    Linux 安装 Nginx
    Linux 常用命令
    springcloud 服务追踪
    Hystrix 服务容错
    Scrum立会报告+燃尽图(十二月十日总第四十一次):用户推广
    Final发布:文案+美工展示博客
  • 原文地址:https://www.cnblogs.com/strengthen/p/10034295.html
Copyright © 2011-2022 走看看