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);
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
R.S
  • 321
  • 3
  • 15
  • If passing your own pointer works, why not stick with that? – nielsbot Oct 01 '12 at 16:55
  • I believe passing null is more optimal, and I may get a drawing performance improvement (which matter a lot for my app). More importantly though, I see an occasional crash where the the app crashes with vm_copy failed and I suspect it may be related to me passing my own pointer (just a gut feeling). – R.S Oct 01 '12 at 17:00
  • Btw--unrelated, but if you performance tune your app and notice a lot of RGBAToARGB calls in your drawing code, try changing your bitmap format to match what CG wants natively. Make sense? This can be different depending on CPU (simulator vs device) – nielsbot Oct 01 '12 at 17:04
  • Sep 19 14:47:35 unknown XYZ[45240] : CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1.Sep 19 14:47:35 unknown UIKitApplication:com.xyz.XYZ[0x91dc][45240] : Sep 19 14:47:35 xyz's-iPad XYZ[45240] : CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1. > Sep 19 14:47:36 unknown ReportCrash[45631] : Formulating crash report for process XYZ[45240] Sep 19 14:47:36 unknown com.apple.launchd[1] : (UIKitApplication:com.xyz.XYZ[0x91dc]) Job appears to have crashed: Segmentation fault: 11 – R.S Oct 01 '12 at 18:00
  • Try setting the size of your allocation to `round_page( original_size )` – nielsbot Oct 01 '12 at 18:23
  • will do. Meanwhile any thoughts on my original question - why is passing NULL not working? – R.S Oct 01 '12 at 21:59
  • Well I'd like to see a backtrace showing where the `vm_copy` is coming from. Maybe breaking on NSLog assuming you don't use that too often could get it. – nielsbot Oct 01 '12 at 22:51
  • ok, based on all my research, using drawrect to draw frequently/repeatedly is a bad idea so I decided to move to UIImageView based drawing, as suggested in a response to this other SO question: http://stackoverflow.com/questions/11261450/drawrect-with-cgbitmapcontext-is-too-slow – R.S Oct 03 '12 at 16:01
  • To add to the above, another SO answer on why UIImageView is more efficient than drawrect. http://stackoverflow.com/questions/8035673/most-efficient-way-to-draw-part-of-an-image-in-ios – R.S Oct 03 '12 at 17:05
  • You might want to post that as an answer to your own question. – nielsbot Oct 03 '12 at 18:37

1 Answers1

0

Based on all my research, using drawrect to draw frequently/repeatedly is a bad idea so I decided to move to UIImageView based drawing, as suggested in a response to this other SO question

Here's another SO answer on why UIImageView is more efficient than drawrect.

Based on the above, I am now using UIImageView instead of drawrect and am seeing better drawing performance.

Community
  • 1
  • 1
R.S
  • 321
  • 3
  • 15