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.
精彩评论