0

I am drawing RGBA data onto the screen using CGBitmapContextCreate and CGContextDrawImage. When I try to create bitmapcontext using CGBitmapContextCreate(pixelBuffer,...) where I have alreadymalloc'ed pixelBuffer and placed my data there, this works just fine.

However, I would like Core Graphics to manage its own memory so I would like to pass NULL to CGBitmapContextCreate, and then get the pointer to the memory block used by calling CGBitmapContextGetData, and copying my RGBA buffer to the aforementioned block using memcpy. However, my memcpy fails. Please see my code below. Any idea what I am doing wrong?

gtx = CGBitmapContextCreate(NULL, screenWidth, screenHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipLast);
void *data = CGBitmapContextGetData(gtx);
memcpy(data, pixelBuffer, area*componentsPerPixel);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(gtx);
CGContextTranslateCTM(currentContext, 0, screenHeight);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawImage(currentContext, currentSubrect, image);
4

1 回答 1

0

根据我的所有研究,使用 drawrect 频繁/重复绘制是一个坏主意,所以我决定转向基于 UIImageView 的绘图,正如对其他 SO question的回复中所建议的那样

这是另一个关于为什么 UIImageView 比 drawrect 更有效的答案。

基于上述情况,我现在使用 UIImageView 而不是 drawrect 并且看到更好的绘图性能。

于 2012-10-05T17:44:15.477 回答