如何在没有内存泄漏的情况下将图像的NSData转换为CGImageRef

时间:2012-05-16 09:26:48

标签: objective-c memory-leaks cgimageref

我将图像的NSData转换为CGImage,如下所示,但是我在那里得到了内存泄漏,但任何人告诉我如何转换它而不会泄漏:

CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
imgData = (CFDataRef)[array objectAtIndex:i];
    imgDataProvider = CGDataProviderCreateWithCFData (imgData);
    image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);

1 个答案:

答案 0 :(得分:10)

这应该有效:

CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
imgData = (CFDataRef)[array objectAtIndex:i];
imgDataProvider = CGDataProviderCreateWithCFData (imgData);
CFRelease(imgData);
image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(imgDataProvider);

完成后,请不要忘记使用CGImageRelease(image1);