以下是三種是獲取當(dāng)前上下文的方式,而繪圖的API有兩種(Core Graphics和OpenGL ES)。
上下文的獲取:UIGraphicsBeginImageContextWithOptions 和 UIGraphicsEndImageContext
這兩個(gè)方法是成對(duì)出現(xiàn)的,函數(shù)塊內(nèi)就是在當(dāng)前上下文中進(jìn)行繪制的。
UIGraphicsBeginImageContextWithOptions(CGMake(100,100),NO,0);
//這里執(zhí)行繪圖操作
//當(dāng)繪制完成后,需要從當(dāng)前上下文中獲取繪制的內(nèi)容,而這個(gè)內(nèi)容是一個(gè)UIImage對(duì)象
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//關(guān)閉圖形上下文
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
? 用法:用來處理圖片的圖形上下文,函數(shù)不僅僅是創(chuàng)建了一個(gè)適用于圖形操作的上下文,并且該上下文也屬于當(dāng)前上下文。
CGSize size: 表示所要?jiǎng)?chuàng)建的圖片的尺寸;
BOOL opaque: 指定所生成圖片的背景是否為不透明,如上我們使用YES而不是NO,則我們得到的圖片背景將會(huì)是黑色;
CGFloat scale: 指定生成圖片的縮放因子,這個(gè)縮放因子與UIImage的scale屬性所指的含義是一致的。傳入0則表示讓圖片的縮放因子根據(jù)屏幕的分辨率而變化。
上下文的獲?。?strong>UIGraphicsPushContext和UIGraphicsPopContext
? 用法:成對(duì)出現(xiàn)的,使用UIGraphicsPushContext來轉(zhuǎn)換已有上下文為當(dāng)前上下文。
//context為已擁有的上下文,必須傳入一個(gè)CGContextRef的上下文,使用UIGraphicsPushContext把傳入的上下文轉(zhuǎn)換為當(dāng)前View的上下文
UIKIT_EXTERN void UIGraphicsPushContext(CGContextRef context);
//繪圖操作
//繪制完成后,需要把轉(zhuǎn)換的上下文恢復(fù),UIGraphicsPopContext函數(shù)恢復(fù)上下文環(huán)境
UIKIT_EXTERN void UIGraphicsPopContext(void);
上下文的獲取:drawRec:
? 當(dāng)drawRect:方法被調(diào)用時(shí),Cocoa就會(huì)為你創(chuàng)建一個(gè)圖形上下文,此時(shí)你對(duì)圖形上下文的所有繪圖操作都是在當(dāng)前View的上下文中 。
//每個(gè)繼承自View的類都包含該方法,可以進(jìn)行繼承該方法來獲取當(dāng)前視圖上下文
- (void) drawRect: (CGRect) rect {
//直接進(jìn)行繪圖操作
}
CGContextSaveGState和CGContextRestoreGState上下文狀態(tài)的壓棧和出棧
//將傳入用于繪制的上下文狀態(tài)壓棧棧頂,并保存?zhèn)魅霑r(shí)的上下文所有狀態(tài)。
void CGContextSaveGState(CGContextRef cg_nullable c)
//將當(dāng)前的上下文狀態(tài)恢復(fù)到傳入的上下文保存時(shí)的狀態(tài)
void CGContextRestoreGState(CGContextRef cg_nullable c)
??:假如當(dāng)前上下文默認(rèn)繪制是黑色的畫筆寬度是2,而我們需要先繪制一個(gè)綠色且寬度為10的線,然后需要繪制一個(gè)綠色且寬度為5的線。這其中就涉及到來修改上下文繪制的畫筆顏色和畫筆寬度屬性,以及恢復(fù)上下文屬性。
- (void) drawView {
CGRect frame = CGRectMake(80, 80, 300, 188);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.backgroundColor = [UIColor groupTableViewBackgroundColor];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//設(shè)置當(dāng)前上下文中繪制的區(qū)域
UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0);
//獲取當(dāng)前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//修改當(dāng)前上下文的畫筆顏色和寬度
CGContextSetLineWidth(context, 2);
[[UIColor blackColor] setStroke];
//保存當(dāng)前上下文的狀態(tài)
CGContextSaveGState(context);
//繪制一個(gè)圓
CGContextAddEllipseInRect(context, CGRectMake(0, 0, frame.size.width, frame.size.height));
CGContextSetLineWidth(context, 10);
[[UIColor greenColor] setStroke];
//開始繪制
CGContextStrokePath(context);
CGContextAddEllipseInRect(context, CGRectMake(10, 10, frame.size.width - 20, frame.size.height - 20));
CGContextSetLineWidth(context, 5);
[[UIColor orangeColor] setStroke];
CGContextStrokePath(context);
//恢復(fù)當(dāng)前上下文的狀態(tài)
CGContextRestoreGState(context);
CGContextAddEllipseInRect(context, CGRectMake(20, 20, frame.size.width - 40, frame.size.height - 40));
//不設(shè)置畫筆顏色和寬度,因?yàn)樵镜纳舷挛挠蓄伾蛯挾葼顟B(tài)
CGContextStrokePath(context);
//從當(dāng)前上下文中獲取繪制的內(nèi)容
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = image;
});
});
[self.view addSubview:imageView];
}

圖片繪制
CGImageCreateWithImageInRect
用法:根據(jù)指定范圍截圖圖片區(qū)域,獲得一個(gè)新的圖片,獲得的圖片是CGImageRef類型的CGImageRef newImageRef = CGImageCreateWithImageInRect(imageRef,size)
CGImageCreateWithImageInRect(CGImageRef cg_nullable image, CGRect rect)
// image: 需要截取的原圖CGImageRef類型
// rect : 截取的區(qū)域
/*
注意:該接口ARC無(wú)效,獲得的圖片需要手動(dòng)釋放.
CGImageRelease(newImageRef)
*/
CGContextDrawImage
用法:在當(dāng)前的上下文中把圖片內(nèi)容繪制到指定區(qū)域
CGContextDrawImage(CGContextRef cg_nullable c, CGRect rect,
CGImageRef cg_nullable image)
// c: 內(nèi)容上下文
// rect : 繪制的區(qū)域
// image: 需要繪制上去的圖片,CGImageRef類型
drawAtPoint
用法:該方法是UIImage類的對(duì)象方法,用于把當(dāng)前圖片按照指定的拋錨點(diǎn)在當(dāng)前上下文中開始繪制
- (void)drawAtPoint:(CGPoint)point;
//point: 圖片開始繪制的起始點(diǎn)
??:
- (void) drawView {
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(40, 80, 180, 120)];
view.backgroundColor = [UIColor groupTableViewBackgroundColor];
UIImage *image = [UIImage imageNamed:@"icon.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 40), NO, 0);
[image drawAtPoint:CGPointMake(0,0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
view.image = newImage;
[self.view addSubview:view];
UIGraphicsEndImageContext();
}

drawInRect
用法:該方法是UIImage類的對(duì)象方法,用于把當(dāng)前圖片按照指定Rect在當(dāng)前上下文中繪制,可以縮放可以位移。用法與drawAtPoint一樣
- (void)drawInRect:(CGRect)rect;
// rect: 指定的繪制Rect
imageWithCGImage: scale: orientation:
用法:該方法是UIImage的類方法,用于把CGImageRef類型的圖片 按照scale與方向轉(zhuǎn)換成對(duì)應(yīng)的UIImage對(duì)象。
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation;
// cgImage: 需要轉(zhuǎn)換的CGImageRef類型的圖片
// scale: 圖片的比例
// orientation: 圖片的方向
UIImageOrientationUp, //默認(rèn)方向

UIImageOrientationDown,// 180 度旋轉(zhuǎn),向下

UIImageOrientationLeft,//向左90度旋轉(zhuǎn)

UIImageOrientationRight,//向右90 度旋轉(zhuǎn)

UIImageOrientationUpMirrored, //按照?qǐng)D片鏡像樣子水平旋轉(zhuǎn)

UIImageOrientationDownMirrored, // 水平翻轉(zhuǎn)

UIImageOrientationLeftMirrored, //向左垂直翻轉(zhuǎn)

UIImageOrientationRightMirrored, //向右垂直翻轉(zhuǎn)
