IOS开发基础之──双缓冲的实现

原理不复杂:

1. 创建一个bitmap context

2. 在bitmap context上画东西

3. 通过bitmapcontext生成一个CGImage

4. 通过UIImage把CGImage内容画到屏幕上

示例代码:

// create the bitmap context

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(nil,27,27,8,0,colorSpace,kCGImageAlphaPremultipliedLast);

CFRelease(colorSpace);

// createan arrow image

// set the fill color

CGColorRef fillColor = [[UIColor blackColor] CGColor];

CGContextSetFillColor(context, CGColorGetComponents(fillColor));

CGContextBeginPath(context);

CGContextMoveToPoint(context, 8.0f, 13.0f);

CGContextAddLineToPoint(context, 24.0f, 4.0f);

CGContextAddLineToPoint(context, 24.0f, 22.0f);

CGContextClosePath(context);

CGContextFillPath(context);

// convert the context into a CGImageRef

CGImageRef image = CGBitmapContextCreateImage(context);

CGContextRelease(context);

UIImage* image2 = [UIImage imageWithCGImage:image];

[image2 drawInRect:CGRectMake(0, 0, 120, 160)];

其中对context绘图的部分可以用来画耗时的3d动画等。

你可能感兴趣的:(IOS开发基础之──双缓冲的实现)