如何使用Core Graphics渲染到屏幕外位图然后blit到屏幕

Fra*_*ger 18 iphone core-graphics bitmap bitblit

我想渲染到一个屏幕外的位图(或RGBA值数组),然后UIView在视图的drawRect函数中将它们blit到a .我更喜欢做完整的32位渲染(包括alpha通道),但也会满足于24位渲染.

有人会介意用一些代码片段或相关API指出我正确的方向吗?

另外,我确切知道如何使用OpenGL完成此操作 - 我更愿意在Core Graphics本身中完成这项工作.

ben*_*ado 15

要渲染到屏幕外的上下文并将其另存为CGImageRef:

void *bitmapData = calloc(height, bytesPerLine);
CGContextRef offscreen = CGBitmapContextCreate(..., bitmapData, ...)
// draw stuff into offscreen
CGImageRef image = CGBitmapContextCreateImage(offscreen);
CFRelease(offscreen);
free(bitmapData);
Run Code Online (Sandbox Code Playgroud)

要在屏幕上绘制它:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, image);
}
Run Code Online (Sandbox Code Playgroud)

您也可以将图像保存在视图的图层内容属性(view.layer.contents = image)中,或使用UIImageView.