Core Graphics之CGContext詳解 (轉)


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];


下圖為場景1和場景2的顯示效果:




場景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];


下圖下放的是剪裁后的圖片效果:

 

原文:http://blog.csdn.net/kingsley_cxz/article/details/9191479


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM