2012-05-16 82 views
4

我轉換圖像的NSData到CGImage如下,但我得到的內存泄漏存在,但任何一個可以告訴我如何轉換與出越來越泄漏:如何NSData的轉換圖像到CGImageRef用了內存泄漏

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

回答

9

這應該工作:

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);你用它做了。

+1

+1。 @laxcha,請閱讀[Core Foundation的內存管理編程指南](https://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html)。 Core Graphics遵循Core Foundation內存管理規則。 –