没有合适的资源?快使用搜索试试~ 我知道了~
iOS图形图像处理,基本功能
资源推荐
资源详情
资源评论
1.
2.
3.
4.
5.
在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框
架。今天我们将围绕iOS中两大图形、图像绘图框架进行介绍:Quartz 2D绘制2D图形和Core Image中强大的滤
镜功能。
Quartz 2D
基本图形绘制
视图刷新
其他图形上下文
Core Image
Quartz 2D
在iOS中常用的绘图框架就是Quartz 2D,Quartz 2D是Core Graphics框架的一部分,是一个强大的二维图像绘
制引擎。Quartz 2D在UIKit中也有很好的封装和集成,我们日常开发时所用到的UIKit中的组件都是由Core
Graphics进行绘制的。不仅如此,当我们引入UIKit框架时系统会自动引入Core Graphics框架,并且为了方便
开发者使用在UIKit内部还对一些常用的绘图API进行了封装。
在iOS中绘图一般分为以下几个步骤:
1.获取绘图上下文
2.创建并设置路径
3.将路径添加到上下文
4.设置上下文状态
5.绘制路径
6.释放路径
图形上下文CGContextRef代表图形输出设备(也就是绘制的位置),包含了绘制图形的一些设备信息,Quartz
2D中的所有对象最终都必须绘制到图形上下文。这样一来,我们在绘制图形时就不必关心具体的设备信息,统
一了代码编写方式(在Quartz 2D中的绘图上下文可以是位图Bitmap、PDF、窗口Window、层Layer、打印对象
Printer)。
基本图形绘制
在UIKit中默认已经为我们准备好了一个图形上下文对象,在UI控件的 方法(这个方法在loadView、drawRect:
viewDidLoad方法后执行)中我们可以通过UIKit封装函数UIGraphicsGetCurrentContext()方法获得这个图形上
下文(注意在其他UI控件方法中无法取得这个对象),然后我们只要按照绘图步骤一步步执行即可。下面自定义
一个KCView继承自UIView,重写drawRect:方法绘制两条直线说明上面绘图的步骤:
KCView.m
//
// KCView.m
// Quartz2D
//
// Created by Kenshin Cui on 14-3-17.
// Copyright (c) 2014 Kenshin Cui. All rights reserved.
//
/**
*/
#import "KCView.h"
@implementation KCView
#pragma mark
//
-(void)drawRect:(CGRect)rect{
//1.
CGContextRef context = UIGraphicsGetCurrentContext();
//2.
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 20, 50);//
CGPathAddLineToPoint(path, nil, 20, 100);//
CGPathAddLineToPoint(path, nil, 300, 100);//
//3.
CGContextAddPath(context, path);
//4.
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1);//
CGContextSetRGBFillColor(context, 0, 1.0, 0, 1);//
CGContextSetLineWidth(context, 2.0);//
CGContextSetLineCap(context, kCGLineCapRound);//,20,50300,100
CGContextSetLineJoin(context, kCGLineJoinRound);//(20,100)
/*
phase:
lengths:838
count:
*/
CGFloat lengths[2] = { 18, 9 };
CGContextSetLineDash(context, 0, lengths, 2);
/*
context:
offset:
blur:
color:
*/
CGColorRef color = [UIColor grayColor].CGColor;//Quartz 2DUIKitUIkit
CGContextSetShadowWithColor(context, CGSizeMake(2, 2), 0.8, color);
//5.
/*CGPathDrawingMode,
kCGPathFill:
kCGPathEOFill:
kCGPathStroke:
kCGPathFillStroke
kCGPathEOFillStroke
*/
CGContextDrawPath(context, kCGPathFillStroke);//
//6.
CGPathRelease(path);
}
@end
在视图控制器创建KCView并添加到根视图中:
- (void)viewDidLoad {
[super viewDidLoad];
KCView *view=[[KCView alloc]initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:view];
}
运行效果如下:
简化绘图方式
上面的绘图方式未免显得有些麻烦,其实Core Graphics 内部对创建对象添加到上下文这两步操作进行了封
装,可以一步完成。另外前面也说过UIKit内部其实封装了一些以“UI”开头的方法帮助大家进行图形绘制。就
拿前面的例子来说我们改进一些绘制方法:
-(void)drawLine2{
//1.
CGContextRef context=UIGraphicsGetCurrentContext();
//2.
CGContextMoveToPoint(context, 20, 50);
CGContextAddLineToPoint(context, 20, 100);
CGContextAddLineToPoint(context, 300, 100);
//:a.,
//CGPathAddLineToPoint(path, nil, 20, 50);
//:b.
CGContextClosePath(context);
//3.
[[UIColor redColor]setStroke];//
[[UIColor greenColor]setFill];//
//[[UIColor blueColor]set];//
//4.
CGContextDrawPath(context, kCGPathFillStroke);
}
上面的操作相比前面的方法应该说已经简化了不少,除了路径之外其他矩形、椭圆等都有对应的创建方法。另
外上面我们也演示了封闭路径的方法,大家可以运行看一下效果。
其他图形绘制
相信大家了解了上面的绘制步骤其他图形绘制并不麻烦,下面以一个例子简单演示一下其他图形的绘制,包括
文字和图像的绘制。
绘制矩形
在下面的方法中还可以看到UIKit对绘图方法的封装,使用起来更加简单。
#pragma mark
-(void)drawRectWithContext:(CGContextRef)context{
//
CGRect rect=CGRectMake(20, 50, 280.0, 50.0);
CGContextAddRect(context,rect);
//
[[UIColor blueColor]set];
//
CGContextDrawPath(context, kCGPathFillStroke);
}
#pragma mark UIKit
-(void)drawRectByUIKitWithContext:(CGContextRef)context{
CGRect rect= CGRectMake(20, 150, 280.0, 50.0);
CGRect rect2=CGRectMake(20, 250, 280.0, 50.0);
//
[[UIColor yellowColor]set];
//,
UIRectFill(rect);//
[[UIColor redColor]setStroke];
UIRectFrame(rect2);//()
}
@end
运行效果:
绘制椭圆
#pragma mark
-(void)drawEllipse:(CGContextRef)context{
//,
CGRect rect=CGRectMake(50, 50, 220.0, 200.0);
CGContextAddEllipseInRect(context, rect);
//
[[UIColor purpleColor]set];
//
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
运行效果:
绘制弧形
-(void)drawArc:(CGContextRef)context{
/*
x:x
y:y
radius:
剩余25页未读,继续阅读
资源评论
北极象
- 粉丝: 1w+
- 资源: 345
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功