Quartz 2D介紹
- 什么是Quartz2D ?
- Quartz 2D是?個(gè)二維繪圖引擎,同時(shí)支持iOS和Mac系統(tǒng)眨猎。
- Quartz 2D能完成的工作希太?
- 繪制圖形 : 線條\三角形\矩形\圓\弧形
- 繪制文字
- 繪制\生成圖片(圖像)
- 讀取\生成PDF
- 截圖\裁剪圖片
- 自定義UI控件
- 其他需要了解的內(nèi)容 占贫?
- Quartz 2D是純C語(yǔ)言的
- Quartz 2D的API來(lái)自于CoreGraphics框架
- 數(shù)據(jù)類(lèi)型和函數(shù)基本都是CG作為前綴
- CGContextRef
- CGPathRef
- CGContextStrokePath(ctx)
- 使用Quartz2D繪圖的基本步驟:
- 自定義view
- 重寫(xiě)drawRect方法
- 1.獲取圖形上下文
- 2.創(chuàng)建路徑對(duì)象(添加路徑)
- 3.渲染
繪制基本圖形
- 繪制一條線段:
- 獲取圖形上下文
- 移動(dòng)到起點(diǎn)
- 添加另一點(diǎn)
- 渲染
// 1.獲取圖形上下文 CGContextRef cxtRef = UIGraphicsGetCurrentContext(); // 2.添加路徑 // 起點(diǎn) CGContextMoveToPoint(cxtRef, 100, 80); // 另外一個(gè)點(diǎn) CGContextAddLineToPoint(cxtRef, 200, 200); // 3.渲染 CGContextStrokePath(cxtRef); // 僅僅畫(huà)線
- 線段效果圖</br>
- 繪制2條線段
// 1.獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 2.添加路徑
// 起點(diǎn)
CGContextMoveToPoint(cxtRef, 50, 50);
// 另外一個(gè)點(diǎn)
CGContextAddLineToPoint(cxtRef, 200, 100);
// 第2條線
CGContextAddLineToPoint(cxtRef, 40, 180);
// 3.渲染
CGContextStrokePath(cxtRef);
- 2條線效果圖</br>
注意:CGContextStrokePath(cxtRef);只是進(jìn)行畫(huà)線,CGContextFillPath(cxtRef)會(huì)將線圍起來(lái)的部分全部畫(huà)出
- 使用CGContextFillPath(cxtRef)的效果</br>
- 繪制三角形
// 1.獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 2.添加路徑
// 起點(diǎn)
CGContextMoveToPoint(cxtRef, 50, 50);
// 另外一個(gè)點(diǎn)
CGContextAddLineToPoint(cxtRef, 200, 50);
// 第2條線
CGContextAddLineToPoint(cxtRef, 125, 180);
// 再向起點(diǎn)連一條線
CGContextAddLineToPoint(cxtRef, 50, 50);
// 3.渲染
CGContextStrokePath(cxtRef); // 僅僅畫(huà)線
- 三角形效果圖</br>
繪制其他基本圖形
通過(guò)UIBezierPath繪制基本圖形
注意:如果在繪制一部分圖形后仍需要繪制圖形着茸,必須將前面的路徑關(guān)閉,再重新開(kāi)始繪制琐旁。
- 三角形及線段
// 1.獲取圖形上下文 CGContextRef cxtRef = UIGraphicsGetCurrentContext(); // 2.圖形路徑 UIBezierPath *bezierPath = [UIBezierPath bezierPath]; [bezierPath moveToPoint:CGPointMake(10, 10)]; [bezierPath addLineToPoint:CGPointMake(200, 200)]; [bezierPath addLineToPoint:CGPointMake(10, 250)]; [bezierPath addLineToPoint:CGPointMake(10, 10)]; // 2.1如果需要再繪制其他的圖形就必須先把當(dāng)前的路徑關(guān)閉 [bezierPath closePath]; // 2.2.再繪制一條線段 [bezierPath moveToPoint:CGPointMake(250, 10)]; [bezierPath addLineToPoint:CGPointMake(250, 290)]; // 需要的是CGPathRef類(lèi)型涮阔,可以通過(guò).CGPath進(jìn)行轉(zhuǎn)換 CGContextAddPath(cxtRef, bezierPath.CGPath); // 3.渲染 CGContextDrawPath(cxtRef, kCGPathStroke);
* 通過(guò)UIBezierPath繪制基本圖形效果圖</br>
![通過(guò)bezierPath繪制基本圖形.png](http://upload-images.jianshu.io/upload_images/1988000-57b79968c4183195.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#### 繪制矩形
```objc
// 1.獲取圖形上下文
CGContextRef ctxRef = UIGraphicsGetCurrentContext();
// 2.創(chuàng)建路徑對(duì)象
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 100)];
// 3.將路徑對(duì)象添加到"圖形上下文"
CGContextAddPath(ctxRef, path.CGPath);
// 4.渲染
CGContextDrawPath(ctxRef, kCGPathStroke);
- 矩形效果圖</br>
繪制圓角矩形
// 2.創(chuàng)建路徑對(duì)象
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 100) cornerRadius:50];
-
圓角矩形效果圖</br>
繪制橢圓
// 2.創(chuàng)建路徑對(duì)象
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 200, 100)];
- 橢圓效果圖</br>
繪制圓弧
- clockWise參數(shù),代表繪制時(shí)YES:是逆時(shí)針繪制 NO:是順時(shí)針繪制
// 2.創(chuàng)建路徑對(duì)象
// 圓心
CGPoint center = CGPointMake(150, 150);
// 半徑
CGFloat radius = 100;
// 開(kāi)始角度
CGFloat startAngle = 0;
// 結(jié)束角度
CGFloat endAngle = M_PI_2;
// 是否為順時(shí)針
BOOL clokcWise = NO;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clokcWise];
- 圓弧效果圖</br>
繪制圓形
// 圓心
CGPoint center = CGPointMake(150, 150);
// 半徑
CGFloat radius = 100;
// 開(kāi)始角度
CGFloat startAngle = 0;
// 結(jié)束角度
CGFloat endAngle = M_PI * 2;
// 是否為順時(shí)針
BOOL clokcWise = YES;
return [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clokcWise];
- 圓形效果圖</br>
繪制常見(jiàn)圖形
- 繪制扇形
- 繪制餅狀圖
- 繪制柱狀圖
- 繪制下載進(jìn)度條
繪制扇形
- 需要繪制弧形灰殴,指定起始角度及結(jié)束角度
- 最后需要將弧形終點(diǎn)與圓心點(diǎn)連起來(lái)敬特,再通過(guò)填充渲染就可以
// 1.獲取圖形上下文 CGContextRef cxtRef = UIGraphicsGetCurrentContext(); // 2.創(chuàng)建弧形的路徑對(duì)象 // 圓心 CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5); // 半徑 CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI_4 clockwise:YES]; // 必須要將終點(diǎn)與圓形連線 [path addLineToPoint:center]; // 4.添加到圖形上下文中 CGContextAddPath(cxtRef, path.CGPath); // 5.渲染
// CGContextDrawPath(cxtRef, kCGPathStroke);
CGContextDrawPath(cxtRef, kCGPathFill);
```
-
扇形效果圖</br>
繪制餅狀圖
- 數(shù)組數(shù)據(jù)
- 通過(guò)計(jì)算每個(gè)元素所占的比例
- 獲取圖形上下文
- 計(jì)算好圓心及半徑
- 遍歷集合數(shù)據(jù)進(jìn)行繪制
- 計(jì)算每個(gè)扇形需要旋轉(zhuǎn)的角度
- 創(chuàng)建路徑對(duì)象進(jìn)行繪制
注意:在繪制時(shí),每個(gè)扇形的終止角度 = 上一個(gè)扇形的結(jié)束角度 + 當(dāng)前扇形所需要的角度
每次遍歷最后都需要將扇形的起始角度置為上一個(gè)扇形的結(jié)束角度
#pragma mark - 繪制餅狀圖
- (void)drawRect:(CGRect)rect {
// 1.數(shù)據(jù)
NSArray *data = @[@25, @15, @10, @5, @30, @10, @5];
// 2.遍歷數(shù)據(jù)進(jìn)行求和
int total = 0;
for (NSNumber *number in data) {
float num = number.floatValue;
total += num;
}
// 3.進(jìn)行繪圖操作
// 3.1獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 3.2遍歷集合進(jìn)行繪圖操作
// 圓心
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
// 半徑
CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5;
// 起始角度和結(jié)束角度
// 注意:要在block內(nèi)修改外部局部變量的值是必須要加__block進(jìn)行修飾的
__block CGFloat startAngle = 0;
__block CGFloat endAngle = 0;
// 遍歷集合
[data enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 轉(zhuǎn)為float類(lèi)型
float num = obj.floatValue;
// 計(jì)算單獨(dú)一個(gè)數(shù)據(jù)結(jié)束角度牺陶,需要用它所占的角度 + 起始角度
endAngle = (num / total) * (M_PI * 2) + startAngle;
// 3.2.1創(chuàng)建路徑對(duì)象
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
// 3.2.2將弧線的終點(diǎn)與圓心連接起來(lái)
[path addLineToPoint:center];
// 3.2.3使用隨機(jī)色進(jìn)行渲染
[[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0] set];
// 3.2.4添加到圖形上下文中
CGContextAddPath(cxtRef, path.CGPath);
// 3.2.5渲染
CGContextDrawPath(cxtRef, kCGPathFill);
// 3.2.6修改起始角度
startAngle = endAngle;
}];
// 4.創(chuàng)建同心圓遮蓋中間部分
UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 70 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
[self.backgroundColor setFill];
CGContextAddPath(cxtRef, arcPath.CGPath);
// 渲染
CGContextDrawPath(cxtRef, kCGPathFill);
}
- 餅狀圖效果圖</br>
繪制柱狀圖
- 計(jì)算所占比例
- 獲取圖形上下文
- 計(jì)算寬度伟阔,遍歷集合進(jìn)行繪制
#pragma mark - 繪制柱狀圖
- (void)drawRect:(CGRect)rect {
// 1.數(shù)據(jù)
NSArray *data = @[@100, @200, @300, @700, @79, @400];
// 2.遍歷數(shù)據(jù)進(jìn)行求和
float total = 0;
for (NSNumber *number in data) {
float num = number.floatValue;
total += num;
}
// 3.進(jìn)行繪圖操作
// 3.1獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 3.2遍歷集合進(jìn)行繪圖操作
// 3.2.1計(jì)算寬度
CGFloat width = rect.size.width / (data.count * 2 - 1);
[data enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 計(jì)算高度
CGFloat height = obj.floatValue / total * rect.size.height * 2;
// 計(jì)算y
CGFloat y = rect.size.height - height;
// 計(jì)算x
CGFloat x = (2 * width) * idx;
// 創(chuàng)建路徑對(duì)象
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, width, height)];
// 添加到圖形上下文中
CGContextAddPath(cxtRef, path.CGPath);
// 設(shè)置隨機(jī)色
[[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0] set];
// 渲染
CGContextDrawPath(cxtRef, kCGPathFill);
}];
}
- 柱狀圖效果圖</br>
繪制下載進(jìn)度條
- 監(jiān)聽(tīng)slider的滑動(dòng)事件
- 將slider的值傳遞給自定義view
- 在自定義view中重寫(xiě)set方法,進(jìn)行繪制
- 繪制時(shí)弧線的結(jié)束角度根據(jù)傳入的進(jìn)度值進(jìn)行計(jì)算
#pragma mark - 繪制下載進(jìn)度條
- (void)drawRect:(CGRect)rect {
// 1.獲取上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 2.創(chuàng)建路徑對(duì)象
// 圓心
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
// 半徑
CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5;
// 起點(diǎn)
CGFloat startAngle = -M_PI_2;
CGFloat endAngle = self.progress * M_PI * 2 - M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path addLineToPoint:center];
#pragma mark - 創(chuàng)建一個(gè)圓形掰伸,利用奇偶填充模式留出中間的空白處
// UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 50 startAngle:startAngle endAngle:endAngle clockwise:YES];
// [path2 addLineToPoint:center];
// 3.添加到上下文中
CGContextAddPath(cxtRef, path.CGPath);
// CGContextAddPath(cxtRef, path2.CGPath);
// 設(shè)置顏色
[[UIColor brownColor] set];
// 4.渲染
CGContextDrawPath(cxtRef, kCGPathFill);
// CGContextDrawPath(cxtRef, kCGPathEOFill);
}
- 下載進(jìn)度條圖效果圖</br>
圖形上下文的矩陣操作
注意: 紅色框?yàn)樯舷挛牡倪吔?/h6>
旋轉(zhuǎn)
#pragma mark - 旋轉(zhuǎn) CTM
// 注意:是繞著左上角進(jìn)行轉(zhuǎn)動(dòng)
CGContextRotateCTM(cxtRef, M_PI_4);
-
旋轉(zhuǎn)上下文</br>
縮放
#pragma mark - 縮放 CTM
// 注意:是以左上角為圓點(diǎn)縮放
CGContextScaleCTM(cxtRef, 0.5, 0.5);
-
縮小上下文</br>
平移
#pragma mark - 平移
CGContextTranslateCTM(cxtRef, 200, -100);
-
平移上下文</br>
-
旋轉(zhuǎn)縮放平移</br>
圖形上下文棧介紹
- 在對(duì)圖形上下文進(jìn)行矩陣操作之前先將圖像上下文最初的狀態(tài)保存起來(lái)皱炉。保存的位置就是圖形上下文的棧。
- 在需要原始狀態(tài)的時(shí)候進(jìn)行再回復(fù)原始的狀態(tài)的狮鸭。
#pragma mark - 矩陣操作
- (void)drawRect:(CGRect)rect {
//獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
//MARK: - 在矩陣操作前保存圖形上下文
CGContextSaveGState(cxtRef);
#pragma mark - 旋轉(zhuǎn) CTM
// 注意:是繞著左上角進(jìn)行轉(zhuǎn)動(dòng)
CGContextRotateCTM(cxtRef, M_PI_4);
#pragma mark - 縮放 CTM
// 注意:是以左上角為圓點(diǎn)縮放
CGContextScaleCTM(cxtRef, 0.5, 0.5);
#pragma mark - 平移
CGContextTranslateCTM(cxtRef, 200, -100); //為什么y值為0 和y值為-100 x值也會(huì)改變 縮放同時(shí)改變位置 位置會(huì)不準(zhǔn)
//創(chuàng)建路徑
//圓形
UIBezierPath *roundPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(180, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
//矩形
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(150, 280, 200, 100)];
//直線
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(50, 50)];
[linePath addLineToPoint:CGPointMake(350, 400)];
//添加路徑
CGContextAddPath(cxtRef, roundPath.CGPath);
CGContextAddPath(cxtRef, rectPath.CGPath);
CGContextAddPath(cxtRef, linePath.CGPath);
//渲染
CGContextDrawPath(cxtRef, kCGPathStroke);
//MARK: - 在執(zhí)行完畢矩陣操作以后合搅,恢復(fù)上下文
CGContextRestoreGState(cxtRef);
//繪制與控制器視圖大小相等的方框方便觀察
UIBezierPath *rectP = [UIBezierPath bezierPathWithRect:rect] ;
//設(shè)置矩形的顏色
[[UIColor redColor] setStroke];
//設(shè)置矩形的線寬
rectP.lineWidth = 10;
[rectP stroke];
}
圖形上下文的內(nèi)存管理
-
CoreGraphics框架里面的使用到"create"和"copy","retain"函數(shù)創(chuàng)建的對(duì)象多搀,最后都需要進(jìn)行釋放
// 1.獲取上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 2.創(chuàng)建路徑對(duì)象
CGMutablePathRef path = CGPathCreateMutable();
// 起點(diǎn)
CGPathMoveToPoint(path, NULL, 50, 50);
// 另一點(diǎn)
CGPathAddLineToPoint(path, NULL, 100, 100);
// 3.將路徑對(duì)象添加到圖形上下文中
CGContextAddPath(cxtRef, path);
// 4.渲染
CGContextDrawPath(cxtRef, kCGPathStroke);
// 5.釋放
// CGPathRelease(path);
#pragma mark - CoreGraphics框架里面的使用到"create"和"copy","retain"函數(shù)創(chuàng)建的對(duì)象,最后都需要進(jìn)行釋放
CFRelease(path);
繪制圖片和文字
繪制圖片
//1.加載圖片
UIImage *mjImage = [UIImage imageNamed:@"頭像"];
//2.1方式1 通過(guò)一個(gè)點(diǎn)開(kāi)始繪制
[mjImage drawAtPoint:CGPointMake(50, 50)];
//2.2方式2 通過(guò)某個(gè)區(qū)域繪制灾部,可能會(huì)壓縮或者拉伸圖片酗昼,使圖片不夠美觀
[mjImage drawInRect:CGRectMake(50, 50, 100, 150)];
//2.3方式3 在某個(gè)區(qū)域內(nèi)以平鋪的方式繪制圖片
[mjImage drawAsPatternInRect:CGRectMake(0, 0, 300, 300)];
繪制文字
//字符串
NSString *str = [NSString stringWithFormat:@"說(shuō)好的不熬夜呢?"];
NSDictionary *dic = @{
NSFontAttributeName : [UIFont systemFontOfSize:20],
NSForegroundColorAttributeName : [UIColor redColor]
};
//方式1. 從某一個(gè)點(diǎn)開(kāi)始繪制
[str drawAtPoint:CGPointMake(50, 50) withAttributes:dic];
//方式2. 在某一個(gè)區(qū)域進(jìn)行繪制 (會(huì)自動(dòng)換行梳猪,但是區(qū)域不要小于字體所占面積大小)
[str drawInRect:CGRectMake(150, 150, 80, 150) withAttributes:dic];
#pragma mark - 旋轉(zhuǎn) CTM
// 注意:是繞著左上角進(jìn)行轉(zhuǎn)動(dòng)
CGContextRotateCTM(cxtRef, M_PI_4);
#pragma mark - 縮放 CTM
// 注意:是以左上角為圓點(diǎn)縮放
CGContextScaleCTM(cxtRef, 0.5, 0.5);
#pragma mark - 平移
CGContextTranslateCTM(cxtRef, 200, -100);
#pragma mark - 矩陣操作
- (void)drawRect:(CGRect)rect {
//獲取圖形上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
//MARK: - 在矩陣操作前保存圖形上下文
CGContextSaveGState(cxtRef);
#pragma mark - 旋轉(zhuǎn) CTM
// 注意:是繞著左上角進(jìn)行轉(zhuǎn)動(dòng)
CGContextRotateCTM(cxtRef, M_PI_4);
#pragma mark - 縮放 CTM
// 注意:是以左上角為圓點(diǎn)縮放
CGContextScaleCTM(cxtRef, 0.5, 0.5);
#pragma mark - 平移
CGContextTranslateCTM(cxtRef, 200, -100); //為什么y值為0 和y值為-100 x值也會(huì)改變 縮放同時(shí)改變位置 位置會(huì)不準(zhǔn)
//創(chuàng)建路徑
//圓形
UIBezierPath *roundPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(180, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
//矩形
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(150, 280, 200, 100)];
//直線
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(50, 50)];
[linePath addLineToPoint:CGPointMake(350, 400)];
//添加路徑
CGContextAddPath(cxtRef, roundPath.CGPath);
CGContextAddPath(cxtRef, rectPath.CGPath);
CGContextAddPath(cxtRef, linePath.CGPath);
//渲染
CGContextDrawPath(cxtRef, kCGPathStroke);
//MARK: - 在執(zhí)行完畢矩陣操作以后合搅,恢復(fù)上下文
CGContextRestoreGState(cxtRef);
//繪制與控制器視圖大小相等的方框方便觀察
UIBezierPath *rectP = [UIBezierPath bezierPathWithRect:rect] ;
//設(shè)置矩形的顏色
[[UIColor redColor] setStroke];
//設(shè)置矩形的線寬
rectP.lineWidth = 10;
[rectP stroke];
}
// 1.獲取上下文
CGContextRef cxtRef = UIGraphicsGetCurrentContext();
// 2.創(chuàng)建路徑對(duì)象
CGMutablePathRef path = CGPathCreateMutable();
// 起點(diǎn)
CGPathMoveToPoint(path, NULL, 50, 50);
// 另一點(diǎn)
CGPathAddLineToPoint(path, NULL, 100, 100);
// 3.將路徑對(duì)象添加到圖形上下文中
CGContextAddPath(cxtRef, path);
// 4.渲染
CGContextDrawPath(cxtRef, kCGPathStroke);
// 5.釋放
// CGPathRelease(path);
#pragma mark - CoreGraphics框架里面的使用到"create"和"copy","retain"函數(shù)創(chuàng)建的對(duì)象,最后都需要進(jìn)行釋放
CFRelease(path);
//1.加載圖片
UIImage *mjImage = [UIImage imageNamed:@"頭像"];
//2.1方式1 通過(guò)一個(gè)點(diǎn)開(kāi)始繪制
[mjImage drawAtPoint:CGPointMake(50, 50)];
//2.2方式2 通過(guò)某個(gè)區(qū)域繪制灾部,可能會(huì)壓縮或者拉伸圖片酗昼,使圖片不夠美觀
[mjImage drawInRect:CGRectMake(50, 50, 100, 150)];
//2.3方式3 在某個(gè)區(qū)域內(nèi)以平鋪的方式繪制圖片
[mjImage drawAsPatternInRect:CGRectMake(0, 0, 300, 300)];
//字符串
NSString *str = [NSString stringWithFormat:@"說(shuō)好的不熬夜呢?"];
NSDictionary *dic = @{
NSFontAttributeName : [UIFont systemFontOfSize:20],
NSForegroundColorAttributeName : [UIColor redColor]
};
//方式1. 從某一個(gè)點(diǎn)開(kāi)始繪制
[str drawAtPoint:CGPointMake(50, 50) withAttributes:dic];
//方式2. 在某一個(gè)區(qū)域進(jìn)行繪制 (會(huì)自動(dòng)換行梳猪,但是區(qū)域不要小于字體所占面積大小)
[str drawInRect:CGRectMake(150, 150, 80, 150) withAttributes:dic];