iOS开发-graphics
CGContextRef的介绍
在CoreGraphics/CGContext.h 的文件里面有这么一句 typedef struct CGContext *CGContextRef;
CGContext是封闭类型,无法再详细查看。
The CGContextRef opaque type represents a Quartz 2D drawing destination. A graphics context contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap image, a PDF document, or a printer. You can obtain a graphics context by using Quartz graphics context creation functions or by using higher-level functions provided in the Carbon, Cocoa, or Printing frameworks. Quartz provides creation functions for various flavors of Quartz graphics contexts including bitmap images and PDF. The Cocoa framework provides functions for obtaining window graphics contexts. The Printing framework provides functions that obtain a graphics context appropriate for the destination printer.
CGContextRef是Quartz 2D的一个绘图上下文。一个绘图上下文包含了绘制参数和所有渲染相关的设备信息,不管目标是应用程序,bitmap图像,PDF文档或者打印机。可以通过Quartz的新建函数创建上下文或者更高级函数提供的上下文。
Quartz 提供的bitmap上下文 CGBitmapContextCreate
UIGraphics 提供的window上下文 UIGraphicsGetCurrentContext(在drawRect中调用)
UIKit
Core Graphics
获取上下文
1、drawRect:
2、drawRect: inContext:
3、UIGraphicsBeginImageContextWithOptions
两大绘图框架的支持以及三种获得图形上下文的方法。
那么我们就有6种绘图的形式。
下面我们开始实现代码,所有的代码都在这个git上。
https://github.com/loyinglin/LearnGraphics
组合1:在drawRect中使用UIKit
#import "UIKitView.h" @implementation UIKitView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 50, 50)]; [[UIColor redColor] setFill]; [path fill]; } @end
组合2:在drawRect中使用CoreGraphics
#import "CoreGraphicsView.h" @implementation CoreGraphicsView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code CGContextRef context = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(context, CGRectMake(10, 10, 50, 50)); CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); CGContextFillPath(context); } @end
组合3:在drawRect: inContext:使用UIKit
组合4:在drawRect: inContext:使用CoreGraphics
#import "ViewDelegate.h" @implementation ViewDelegate - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { NSLog(@"ABC %@", ctx); UIGraphicsPushContext(ctx); UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 50, 50)]; [[UIColor redColor] setFill]; [path fill]; UIGraphicsPopContext();
//以下是使用CoreGrahpics CGContextAddEllipseInRect(ctx, CGRectMake(30, 30, 20, 20)); CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor); CGContextFillPath(ctx); } @end
组合5:在UIGraphicsBeginImageContextWithOptions使用UIKit
组合6:在UIGraphicsBeginImageContextWithOptions使用CoreGraphics
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, NO, 0); UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 50, 50)]; [[UIColor redColor] setFill]; [path fill]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); dispatch_async(dispatch_get_main_queue(), ^{ [self.myImageView setImage:image]; }); UIGraphicsEndImageContext(); UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, NO, 0); //core started CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, CGRectMake(10, 10, 20, 20)); CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor); CGContextFillPath(ctx); CGContextFillRect(ctx, CGRectMake(30, 30, 20, 20)); image = UIGraphicsGetImageFromCurrentImageContext(); dispatch_async(dispatch_get_main_queue(), ^{ [self.myImageView2 setImage:image]; }); //end UIGraphicsEndImageContext(); });
参考
离屏渲染 http://www.tuicool.com/articles/2iYbMfE
iOS绘图详细教程 http://www.cocoachina.com/industry/20140115/7703.html