- Quartz 2D的功能
- 繪制圖形:線條、三角形、矩形倦始、圓王浴、弧等脆炎。
- 繪制文字
- 繪制\生成圖片(圖像)
- 讀取\生成PDF
- 截圖
- 自定義UI控件
- 使用步驟
- 獲取一個(gè)與視圖相關(guān)的上下文
- 創(chuàng)建一個(gè)可變路徑(本身是有一個(gè)默認(rèn)路徑的)
1.創(chuàng)建路徑
2.設(shè)置路徑起點(diǎn)
3.增加路徑內(nèi)容 - 將路徑添加到上下文
- 設(shè)置上下文狀態(tài)
1.設(shè)置邊線顏色
2.設(shè)置填充顏色
3.設(shè)置線寬
4.設(shè)置線段連接樣式
5.設(shè)置線段首位樣式
6.設(shè)置虛線樣式 - 繪制路徑
- 釋放路徑
繪制圖形
- (void)drawRect:(CGRect)rect
{
// [self drawLine];
// [self drawLine_2];
// [self drawRect];
// [self drawRect_2];
// [self drawArc];
// [self drawCircle];
// [self drawGradient];
// [self drawText];
// [self drawImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[self createImage]];
[self addSubview:imageView];
}
- (void)drawLine{
/*
當(dāng)前視圖上下文
*/
CGContextRef context = UIGraphicsGetCurrentContext(); //獲取視圖上下文
/*
構(gòu)建路徑
*/
CGMutablePathRef path = CGPathCreateMutable(); //創(chuàng)建路徑
CGPathMoveToPoint(path, NULL, 50, 50); //設(shè)置路徑起點(diǎn)
CGPathAddLineToPoint(path, NULL, 50, 420); //路徑內(nèi)容
CGPathAddLineToPoint(path, NULL, 270, 420);
CGPathAddLineToPoint(path, NULL, 270, 50);
CGPathCloseSubpath(path); //關(guān)閉路徑
/*
將路徑添加到上下文
*/
CGContextAddPath(context, path);
/*
設(shè)置上下文狀態(tài)
*/
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1); //設(shè)置邊線顏色
CGContextSetRGBFillColor(context, 0, 0, 1, 1); //設(shè)置填充顏色
CGContextSetLineWidth(context, 10); //設(shè)置線寬
CGContextSetLineJoin(context, kCGLineJoinBevel); //設(shè)置線段連接樣式
CGContextSetLineCap(context, kCGLineCapSquare); //設(shè)置線段首位樣式
CGFloat lengths[] = {20,100};
CGContextSetLineDash(context, 100, lengths, 2); //設(shè)置虛線
/*
繪制路徑
*/
CGContextDrawPath(context, kCGPathEOFill);
/*
釋放路徑(如果創(chuàng)建對象的函數(shù)中包含create,copy或者retain字樣氓辣,就需要釋放秒裕!)
*/
CGPathRelease(path);
}
- (void)drawLine_2
{
/*
當(dāng)前視圖上下文
*/
CGContextRef context = UIGraphicsGetCurrentContext(); //獲取視圖上下文
/*
構(gòu)建路徑
*/
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 50);
/*
保存當(dāng)前上下文
*/
CGContextSaveGState(context);
/*
設(shè)置上下文狀態(tài)
*/
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1); //設(shè)置邊線顏色
CGContextSetLineWidth(context, 10); //設(shè)置線寬
CGContextSetLineJoin(context, kCGLineJoinBevel); //設(shè)置線段連接樣式
CGContextSetLineCap(context, kCGLineCapSquare); //設(shè)置線段首位樣式
/*
繪制路徑
*/
CGContextStrokePath(context);
/*
恢復(fù)上下文
*/
CGContextRestoreGState(context);
/*
再次繪制
*/
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 50);
CGContextStrokePath(context);
}
- (void)drawRect
{
CGContextRef context = UIGraphicsGetCurrentContext(); //獲取視圖上下文
CGContextAddRect(context, CGRectMake(50, 50, 100, 100));
CGContextFillPath(context);
}
- (void)drawRect_2
{
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor redColor] setFill];
[[UIColor darkGrayColor] setStroke];
CGContextFillRect(context, CGRectMake(50, 50, 100, 100));
CGContextStrokeRect(context, CGRectMake(50, 50, 100, 100));
}
- (void)drawArc
{
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor redColor] set];
/*
添加圓弧
*/
CGContextAddArc(context, 160, 230, 100, 0, M_PI_2, 0);
/*
繪制路徑
*/
CGContextStrokePath(context);
}
- (void)drawCircle
{
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor greenColor] setFill];
CGContextAddEllipseInRect(context, CGRectMake(50, 50, 200, 200));
CGContextFillPath(context);
}
- (void)drawGradient
{
// 1. 定義漸變引用CGGradientRef
CGGradientRef gradient;
// 2. 定義色彩空間引用
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// 3. 定義漸變顏色組件
// 每四個(gè)數(shù)一組,分別對應(yīng)r,g,b,透明度
CGFloat components[8] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0};
// 4. 定義顏色漸變位置
// 第一個(gè)顏色開始漸變的位置
// 第二個(gè)顏色結(jié)束漸變的位置
CGFloat locations[2] = {0, 1};
// 5. 創(chuàng)建顏色漸進(jìn)
gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
// 6. 創(chuàng)建貝塞爾路徑钞啸,是OC的几蜻,如果只是制定了漸變,沒有指定剪切路徑体斩,就是整個(gè)視圖的漸變
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 200)];
// 7. 添加剪切路徑
[path addClip];
// 8. 繪制線性漸進(jìn)
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(context, gradient, CGPointMake(50, 50), CGPointMake(200, 50), kCGGradientDrawsAfterEndLocation);
// 9. 釋放顏色空間
CGColorSpaceRelease(colorSpace);
// 10. 釋放漸變引用
CGGradientRelease(gradient);
}
- (void)drawText
{
NSString *text = @"I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!I Love you!";
NSLog(@"%@",[UIFont familyNames]);
// 使用set既指定邊框顏色梭稚,又指定填充顏色
[[UIColor redColor]set];
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:30];
[text drawAtPoint:CGPointMake(50, 50) withFont:font];
CGRect rect = CGRectMake(50, 200, 200, 200);
[[UIColor blueColor]set];
UIRectFill(rect);
[[UIColor redColor]set];
// 只能用居中、左絮吵、右對齊
[text drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}
- (void)drawImage
{
UIImage *image = [UIImage imageNamed:@"1.png"];
// 在指定點(diǎn)繪制
[image drawAtPoint:CGPointMake(50, 50)];
// 會拉伸
[image drawInRect:CGRectMake(0, 0, 320, 460)];
// 平鋪
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 460)];
}
- (UIImage *)createImage
{
// 1. 獲得圖像相關(guān)的上下文
// 獲得圖像上下文的時(shí)候弧烤,需要指定上下文大小
UIGraphicsBeginImageContext(CGSizeMake(320, 200));
// 2. 繪制圖像
UIImage *image = [UIImage imageNamed:@"1.png"];
[image drawInRect:CGRectMake(0, 0, 320, 200)];
// 3. 寫水印文字
NSString *text = @"水印文字";
// [[UIColor whiteColor]set];
// 新建一個(gè)UIColor
UIColor *color = [UIColor redColor];
[color set];
[text drawInRect:CGRectMake(0, 170, 300, 20) withFont:[UIFont systemFontOfSize:12] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentRight];
// 從圖像上下文中獲得當(dāng)前繪制的結(jié)果,并生成圖像
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
// 4. 關(guān)閉上下文
UIGraphicsEndImageContext();
// 5. 把圖像歸檔蹬敲,可以用這個(gè)方法來做縮略圖
// NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *path = [documents[0]stringByAppendingPathComponent:@"image.png"];
NSString *path = @"/Users/apple/Desktop/image.png";
NSData *imageData = UIImagePNGRepresentation(result);
[imageData writeToFile:path atomically:YES];
return result;
}