I'm trying to retrieve the image data from one uiimage, modify it, and create a new uiimage from it. To start, I tried just copying the data without any modification to get the basics down - but that fails (UIImage +imageWithData:
returns nil). Can anyone see why this wouldn't work?
// I've confirmed that the follow line works
UIImage *image = [UIImage imageNamed:@"image_foo.png"];
// Get a reference to the data, appears to work
CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
// Get the length of memory to allocate, seems to work (e.g. 190000)
int length = CFDataGetLength(dataRef) * sizeof(UInt8);
UInt8 * buff = malloc(length);
// Again, appears to work
CFDataGetBytes(dataRef, CFRangeMake(0,length),buff);
// Again, appears to work
NSData * newData = [NSData dataWithBytesNoCopy:buff length:length];
// This fails by returning nil
UIImage *image2 = [UIImage imageWithData:newData];
Note:
I also tried using:
UInt8* data = CFDataGetBytePtr(dataRef);
and just piping that straight into NSData.
Same result.