Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

iphone - How to render to offscreen bitmap then blit to screen using Core Graphics

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

To draw it on the screen:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...