iOS两大绘图框架:UIKit像UIImage、NSString(绘制文本)、UIBezierPath(绘制形状)、UIColor都知道如何绘制自己。 这些类提供了功能有限但使用方便的方法来让我们完成绘图任务。一般情况下,UIKit就是我们所需要的。 Core Graphics 这是一个绘图专用的API族,它经常被称为QuartZ或QuartZ2D。Core Graphics是iOS上所有绘图 功能的基石,包括UIKit。
本文主要介绍如何通过UIKit绘图。
1.drawRect:方法
-(void)drawRect:(CGRect)rect{
///绘制正方形路径
// UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50, 50)];
/// 绘制直径为50的圆
// UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 50)];
/// 绘制宽高为50,边角弧度为10的弧边矩形
// UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:10];
/// 绘大小为RoundedRect 的矩形,圆角切弧为cornerRadii 位置在UIRectCornerBottomRight
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 10, 50, 50) byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 5)];
///绘制以(0,0)为圆心,半径为40,角度从0.2到0.5的圆弧,closewise为圆弧边是否封闭
// UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:40 startAngle:0.2 endAngle:0.5 clockwise:NO];
[[UIColor blueColor]setFill];
[path fill];
}
2.-(void)drawInContext:(CGContextRef)ctx;方法
(a).自定义一个继承于CALayer类
@interface myLayer : CALayer
@end
@implementation myLayer
-(void)drawInContext:(CGContextRef)ctx{
UIGraphicsPushContext(ctx);
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 100, 150)];
[[UIColor greenColor]setFill];
[path fill];
UIGraphicsPopContext();
}
@end
(2)将自定义layer添加到主视图layer上
myLayer * layer = [myLayer layer];
layer.backgroundColor = [[UIColor redColor]CGColor];
layer.frame = CGRectMake(10, 100, 200,200);
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
3.使用- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx;
(1).创建对象,实现绘图方法
@interface thridDrawLayerDelegate : NSObject
@end
@implementation thridDrawLayerDelegate
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
UIGraphicsPushContext(ctx);
UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
[[UIColor blueColor] setFill];
[p fill];
UIGraphicsPopContext();
}
(2).添加到layer视图上
@interface ThridDrawViewController ()
{
CALayer * _layer;
thridDrawLayerDelegate *_layerDeleagete;
}
@end
@implementation ThridDrawViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_layerDeleagete = [[thridDrawLayerDelegate alloc] init];
//1.创建自定义的layer
_layer=[CALayer layer];
//2.设置layer的属性
_layer.backgroundColor= [UIColor blackColor].CGColor;
_layer.frame=CGRectMake(100, 100, 200, 200);
_layer.delegate = _layerDeleagete;
[_layer setNeedsDisplay];
//3.添加layer
[self.view.layer addSublayer:_layer];
}
-(void)dealloc{
_layer.delegate = nil;
}
4.使用UIGraphicsBeginImageContextWithOptions生成图片;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, 0);
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];
[[UIColor greenColor] setFill];
[path fill];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
github下载地址