I’m trying to take a CGImage and copy its data into a buffer for later processing. The code below is what I have so far, but there’s one thing I don’t like about it – it’s copying the image data twice. Once for CGDataProviderCopyData() and once for the :getBytes:length call on imgData. I haven’t been able to find a way to copy the image data directly into my buffer and cut out the CGDataProviderCopyData() step, but there has to be a way…any pointers? (…pun ftw)
NSData *imgData = (NSData *)(CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef))); CGImageRelease(myCGImageRef); // i've got a previously-defined pointer to an available buffer called "mybuff" [imgData getBytes:mybuff length:[imgData length]];
See this answer:
Does CGDataProviderCopyData() actually copy the bytes? Or just the pointer?
Looks like direct access to the buffer behind a CGDataProvider is restricted to private APIs.