问题
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]];
回答1:
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.
回答2:
Unless you see a performance problem when you use Instruments to measure it, I don't think that copying it twice is a huge problem. You could always store the CFDataRef imgData directly instead of the mybuff.
Edit
I'm talking about this kind of code:
In your class interface add an array:
NSMutableArray *images;
In your init: method: create the array:
images = [[NSMutableArray alloc] init];
and have a dealloc;
-(void)dealloc{
[images release];
}
and in your code that saves each image:
CGDataRef imageDataRef = CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef)));
[images addObject: (NSData*)imageDataRef];
CGRelease(imageDataRef);
来源:https://stackoverflow.com/questions/2740173/iphone-get-a-pointer-to-the-data-behind-cgdataprovider