views:

67

answers:

1

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 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];
A: 

In practice,

UIImageXXXRepresentation([UIImage imageWithCGImage:imageRef])

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

ORYLY