CGContext又叫圖形高低文,相當於一塊畫布,以客棧情勢存放,只有在當前 context上畫圖纔有效。iOS有分多種圖形高低文,此中UIView自帶供給的在drawRect:辦法中經由過程 UIGraphicsGetCurrentContext獲取,還有專門爲處理懲罰的context,UIGraphicsBeginImageContext函數生成,還有pdf的context等等。
1.一共有3種應用context的場景,此中每種場景都有2種辦法畫圖
場景1:
//經由過程UIView的子類的drawRect:在高低文中繪製,該辦法體系已籌辦好一個cgcontext,並放置在高低文棧頂,rect形參就是context的尺寸大小
//當一個UIView的backgroundColor爲nil和opaque爲YES時,產生的context的靠山就爲黑色的
- (void)drawRect:(CGRect)rect
{
//1.應用UIKit在context上繪製,UIKit的所有操縱只會在當前棧頂的context,所以須要重視當前棧頂的context是否你須要操縱的高低文
//UIImage,NSString,UIBezierPath,UIColor等可以直接在當前context上操縱
UIImage* image = [UIImage imageNamed:@"test.png"];
NSLog(@"size:%@",NSStringFromCGSize(image.size));
//UIImage直接在context上操縱,指定在context的哪個座標上繪製,大小是原圖的尺寸,若是大小超出了context的侷限就會被截取掉
// [image drawAtPoint:CGPointMake(100, 100)];
//指定在context的哪個座標上繪製,並指定繪製的尺寸大小,如許的尺寸就會被緊縮,不會超出context侷限
[image drawInRect:CGRectMake(0, 0, rect.size.width/2, rect.size.height/2)];
//2.應用Core Graphics的函數在context上繪製,Core Graphics的函數須要context作爲參數,只繪製在指定應用的context上
//功過UIGraphicsGetCurrentContext函數獲取當前高低文棧頂的context,UIView體系已爲其籌辦好context並存放在棧頂了
// CGContextRef context = UIGraphicsGetCurrentContext();
// //畫一個橢圓
// CGContextAddEllipseInRect(context, CGRectMake(0,0,100,100));
// //填充色彩爲藍色
// CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
// //在context上繪製
// CGContextFillPath(context);
場景2:
//實現該辦法,用於CALayer回調,CALayer經由過程它的類來進行畫圖操縱,切記切切不克不及把UIView作爲CALayer的類,因爲UIView自身有隱式的圖層,若再把顯式的圖層賦給它會產生不有名錯誤的
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx
{
//1.應用UIKit進行繪製,因爲UIKit只會對當前高低文棧頂的context操縱,所以要把形參中的context設置爲當前高低文
UIGraphicsPushContext(ctx);
UIImage* image = [UIImage imageNamed:@"test.png"];
//指定地位和大小繪製
[image drawInRect:CGRectMake(0, 0,100 , 100)];
UIGraphicsPopContext();
// UIGraphicsPushContext(ctx);
//2.應用Core Graphics進行繪製,須要顯式應用context
// //畫一個橢圓
// CGContextAddEllipseInRect(ctx, CGRectMake(0,0,100,100));
// //填充色彩爲藍色
// CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
// //在context上繪製
// CGContextFillPath(ctx);
// UIGraphicsPopContext();
}
LayerDelegate* delegate = [[LayerDelegate alloc]init];
CALayer* layer = [CALayer layer];
layer.anchorPoint = CGPointMake(0, 0);
layer.position = CGPointMake(100, 100);
layer.bounds = CGRectMake(0, 0, 200, 200);
layer.delegate = delegate;
//須要顯式調用setNeedsDisplay來刷新纔會繪製layer
[layer setNeedsDisplay];
[self.view.layer addSublayer:layer];
場景3:
//經由過程本身創建一個context來繪製,凡是用於對的處理懲罰
/*
申明一下UIGraphicsBeginImageContextWithOptions函數參數的含義:第一個參數默示所要創建的的尺寸;第二個參
數用來指定所生成的靠山是否爲不透明,如上我們應用YES而不是NO,則我們獲得的靠山將會是黑色,顯然這不是我想要的;第三個參數指定生成
的縮放因子,這個縮放因子與UIImage的scale屬性所指的含義是一致的。傳入0則默示讓的縮放因子按照屏幕的辨別率而變更,所以我們獲得的圖
片不管是在單辨別率還是視網膜屏上看起來都邑很好。
*/
//該函數會主動創建一個context,並把它push到高低文棧頂,座標系也經處理懲罰和UIKit的座標系雷同
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(0,0,100,100));
//填充色彩爲藍色
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
//在context上繪製
CGContextFillPath(context);
//把當前context的內容輸出成一個UIImage
UIImage* i = UIGraphicsGetImageFromCurrentImageContext();
//高低文棧pop出創建的context
UIGraphicsEndImageContext();
[i drawInRect:CGRectMake(0, 0, 100, 100)];
2.把全部屏幕轉化爲
UIImageView* imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0,
0, self.view.frame.size.width, self.view.frame.size.height)];
UIGraphicsBeginImageContextWithOptions(imageV.frame.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//把當前的全部畫面導入到context中,然後經由過程context輸出UIImage,如許就可以把全部屏幕轉化爲
[self.view.layer renderInContext:context];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
imageV.image = image;
UIGraphicsEndImageContext();
3.剪裁
//對一張進行剪裁
CGImageRef imageref = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(100, 100, 200, 50));
UIImageView* cropImage = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 50)];
cropImage.image = [UIImage imageWithCGImage:imageref];
CGImageRelease(imageref);
[self.view addSubview:cropImage];