开发者

Is there a thread safe way to create PNG images in pre-4.0 iOS?

开发者 https://www.devze.com 2023-01-15 01:05 出处:网络
I\'m pretty sure that UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]) is the cause of random-looking memory leaks when done in a background thread (it causes leaks that trace back to

I'm pretty sure that

UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef])

is the cause of random-looking memory leaks when done in a background thread (it causes leaks that trace back to CGContextDrawPDFPage!).

Now, everywhere else on the internet says I should use CGImageDestination, which isn't available until iOS4. Is there any way for me to encode the bitmap as PNG other than importing heavyweight PNG libraries?

EDIT: Now this is interesting. For the whole background thread that creates the PNGs, I drain the autorelease pool every 10 开发者_JAVA技巧generated PNGs. The memory warnings and the crash disappear after I add an autorelease pool around the saving. Are these calls that memory hungry?

NSAutoreleasePool* savePool = [[NSAutoreleasePool alloc] init];

NSData* imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
[imageData writeToFile:savePath atomically:NO];

[savePool drain];


In practice,

UIImageXXXRepresentation([UIImage imageWithCGImage:imageRef])

seems to be thread safe enough. Just look out for mem usage.

0

精彩评论

暂无评论...
验证码 换一张
取 消