在 4.0 之前的 iOS 中是否有线程安全的方法来创建 PNG 图像?
我很确定这
UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef])
就是在后台线程中完成时出现随机内存泄漏的原因(它会导致泄漏追溯到 CGContextDrawPDFPage!)。
现在,互联网上的其他地方都说我应该使用 CGImageDestination,它直到 iOS4 才可用。除了导入重量级 PNG 库之外,还有什么方法可以将位图编码为 PNG 吗?
编辑:现在这很有趣。对于创建 PNG 的整个后台线程,我每生成 10 个 PNG 就会耗尽自动释放池。在我在保存周围添加自动释放池后,内存警告和崩溃消失了。这些调用是否占用内存?
NSAutoreleasePool* savePool = [[NSAutoreleasePool alloc] init];
NSData* imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef]);
[imageData writeToFile:savePath atomically:NO];
[savePool drain];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在实践中,
线程似乎足够安全。只需留意内存的使用情况即可。
In practice,
seems to be thread safe enough. Just look out for mem usage.