渲染到离屏位图然后

渲染到离屏位图然后

本文介绍了如何使用 Core Graphics 渲染到离屏位图然后 blit 到屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想渲染到屏幕外位图(或 RGBA 值数组),然后在视图的 drawRect 函数中将它们渲染到 UIView.我更愿意进行完整的 32 位渲染(包括 alpha 通道),但也满足于 24 位渲染.

I would like to render to an offscreen bitmap (or array of RGBA values) and then blit those to a UIView during in the view's drawRect function. I would prefer to do full 32-bit rendering (including alpha channel), but would also be content with 24-bit rendering.

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

Would anyone mind pointing me in the right direction with some code snippets or relevant APIs?

此外,我确切地知道如何使用 OpenGL 执行此操作 - 我只是更喜欢在 Core Graphics 本身中执行此工作.

Also, I know exactly how to do this using OpenGL - I would just prefer to do this work in Core Graphics itself.

推荐答案

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

To render into an offscreen context and save it as a CGImageRef:

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

在屏幕上绘制:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, image);
}

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

You could also just save the image in the view's layer's contents property (view.layer.contents = image), or use a UIImageView.

这篇关于如何使用 Core Graphics 渲染到离屏位图然后 blit 到屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:27