我试图从一个UIImage中检索图像数据,对其进行修改,然后从中创建一个新的UIImage。 首先,我尝试仅复制数据而不进行任何修改以获得基础知识 - 但该方法失败了(
注意:
我也尝试使用:
UInt8* data = CFDataGetBytePtr(dataRef);
并将其直接传输到NSData中。
结果相同。
UIImage + imageWithData:
返回nil)。 有谁能看出这为什么行不通?// 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];
注意:
我也尝试使用:
UInt8* data = CFDataGetBytePtr(dataRef);
并将其直接传输到NSData中。
结果相同。