滤镜
一、利用CoreImage来处理图像
Core Image是一个很强大的框架。它可以让你简单地应用各种滤镜来处理图像,比如修改鲜艳程度, 色泽, 或者曝光。 它利用GPU(或者CPU,取决于客户)来非常快速、甚至实时地处理图像数据和视频的帧。多个Core Image滤镜可以叠加在一起,从而可以一次性地产生多重滤镜效果。这种多重滤镜的优点在于它可以生成一个改进的滤镜,从而一次性的处理图像达到目标效果,而不是对同一个图像顺序地多次应用单个滤镜。每一个滤镜都有属于它自己的参数。这些参数和滤镜信息,比如功能、输入参数等都可以通过程序来查询。用户也可以来查询系统从而得到当前可用的滤镜信息。到目前为止,Mac上只有一部分Core Image滤镜可以在iOS上使用。但是随着这些可使用滤镜的数目越来越多,API可以用来发现新的滤镜属性。
Core Image 总览
Core Image框架中最重要的几个类:
CIContext. 所有图像处理都是在一个CIContext 中完成的,这很像是一
Core Image处理器或是OpenGL的上下文。
CIImage. 这个类保存图像数据。它可以从UIImage、图像文件、或者是像素数据中构造出来。
CIFilter. 滤镜类包含一个字典结构,对各种滤镜定义了属于他们各自的属性。滤镜有很多种,比如鲜艳程度滤镜,色彩反转滤镜,剪裁滤镜等等。
二、绘图与滤镜参考博客:
1.http://blog.sina.com.cn/s/blog_6f29e81f010176lr.html
2.http://doc.okbase.net/kenshincui/archive/97786.html
滤镜样式
["CILinearToSRGBToneCurve",
"CIPhotoEffectChrome",
"CIPhotoEffectFade",
"CIPhotoEffectInstant",
"CIPhotoEffectMono",
"CIPhotoEffectNoir",
"CIPhotoEffectProcess",
"CIPhotoEffectTonal",
"CIPhotoEffectTransfer",
"CISRGBToneCurveToLinear",
"CIVignetteEffect"]
滤镜Demo
import UIKit
class ViewController: UIViewController {
var scrollView: UIScrollView!
let screenW = UIScreen.mainScreen().bounds.size.width
let screenH = UIScreen.mainScreen().bounds.size.height
override func viewDidLoad() {
super.viewDidLoad()
//滤镜的样式 - Key是系统的,只是一部分
let filterArray = ["CILinearToSRGBToneCurve",
"CIPhotoEffectChrome",
"CIPhotoEffectFade",
"CIPhotoEffectInstant",
"CIPhotoEffectMono",
"CIPhotoEffectNoir",
"CIPhotoEffectProcess",
"CIPhotoEffectTonal",
"CIPhotoEffectTransfer",
"CISRGBToneCurveToLinear",
"CIVignetteEffect"]
//创建scrollView
self.scrollView = UIScrollView(frame: CGRectMake(0,0,screenW,screenH))
scrollView.pagingEnabled = true
self.view.addSubview(scrollView)
//设置contentSize
scrollView.contentSize = CGSizeMake(CGFloat(filterArray.count) * screenW, 0)
for i in 0.. Void in
//处理图片
//定义空数组用于承接处理后的图片
let imageArray = NSMutableArray()
for i in 0.. Void in
for i in 0..
绘画
绘画之前有提到过贝塞尔曲线,这里使用其他方式——系统CoreGraphics
步骤1:在controller里面添加一个自定义的View
import UIKit
//CoreGraphics、CoreData、CoreAnimation都属于CF(core foundation)框架
class ViewController: UIViewController {
var customView = CustomView()
override func viewDidLoad() {
super.viewDidLoad()
//CoreGraphics是系统提供的一组绘图的API,他的实现需要调用系统的drawRect方法
//drawRect方法调用时机:
//1.必须在添加到父视图之后
//2.drawRect方法在viewWillAppear之后调用(didload->willAppear->drawRect)
customView.frame = self.view.bounds
customView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(customView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
}
步骤2.在自定义的类里面完成绘画
import UIKit
class CustomView: UIView {
override func drawRect(rect: CGRect) {
//上下文——画布
let context = UIGraphicsGetCurrentContext()
//设置画笔颜色
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
//设置会直线条宽度
CGContextSetLineWidth(context, 5.0)
// //绘制单线条
// drawSigleLine(context!)
//
// //绘制多线条
// drawLines(context!)
//
// //绘制虚线
// drawDash(context!)
//
// //绘制矩形
// drawSquare(context!)
//
// //绘制椭圆
// drawCirle(context!)
//
//绘制文字
drawText(context!)
//绘制图片
//drawImage(context!)
}
func drawSigleLine(context:CGContextRef){
//设置起点
CGContextMoveToPoint(context, 10, 10)
//设置终点并进行绘画
CGContextAddLineToPoint(context, 200, 200)
CGContextStrokePath(context)
}
func drawLines(context:CGContextRef){
//设置起点
CGContextMoveToPoint(context, 10, 10)
//第一条线
CGContextAddLineToPoint(context, 200, 200)
//第二条线
CGContextAddLineToPoint(context, 100, 300)
//第三条线
//CGContextAddLineToPoint(context, 10, 10)
CGContextClosePath(context)
CGContextStrokePath(context)
}
func drawDash(context:CGContextRef){
//设置虚线样式
let lengths: [CGFloat] = [10,20]
//参数2:虚线开始的位置
//参数2:虚线的样式
//参数3:数组中元素个数
CGContextSetLineDash(context, 0, lengths, 2)
//设置起点
CGContextMoveToPoint(context, 10, 10)
//设置终点并进行绘画
CGContextAddLineToPoint(context, 200, 200)
CGContextStrokePath(context)
}
func drawSquare(context:CGContextRef){
//方式1
CGContextStrokeRect(context, CGRectMake(10, 10, 100, 100))
//方式2
CGContextAddRect(context, CGRectMake(200, 200, 100, 200))
CGContextStrokePath(context)
//方式3
//绘制填充矩形
UIColor.blueColor().setFill()
CGContextFillRect(context, CGRectMake(10, 200, 100, 200))
}
func drawCirle(context:CGContextRef){
//方式1
CGContextStrokeEllipseInRect(context, CGRectMake(0,0,200,200))
//方式2 - 填充
UIColor.grayColor().setFill()
CGContextFillEllipseInRect(context, CGRectMake(100, 200, 200, 100))
}
func drawText(context:CGContextRef){
let str = "就不是大法官事故的发生的风景好感动是飞洒的复原图地方刚送到附近开始的功夫感到十分敏感是的覅" as NSString
//方式1:设置起点
//弊端:只能显示一行
//str.drawAtPoint(CGPointMake(10, 10), withAttributes: [NSFontAttributeName:UIFont.systemFontOfSize(15),NSForegroundColorAttributeName:UIColor.blueColor()])
//方式2:设置大小 - 将文字平铺在矩形框内
str.drawInRect(CGRectMake(0, 0, 200, 200), withAttributes: [NSFontAttributeName:UIFont.systemFontOfSize(15),NSForegroundColorAttributeName:UIColor.blueColor()])
}
func drawImage(context:CGContextRef){
let image = UIImage(named: "49.jpg")
image?.drawInRect(CGRectMake(0, 0, 300, 300))
}
}