在iPhone应用程序上,我需要通过邮件发送一个最大大小为300Ko的mail.app (我不知道jpg的最大大小,但这是另一个问题)。为了做到这一点,我试图降低质量,直到获得低于300Ko的图像。
为了获得好的质量(compressionLevel)谁给我一个低于300Ko的jpg,我做了以下循环。它正在工作,但每次执行循环时,内存都会增加原始jpg (700Ko)的大小,尽管有"tmpImage release;“。
float compressionLevel = 1.0f;
int size = 300001;
while (size > 300000) {
UIImage *tmpImage =[[UIImage alloc] initWithContentsOfFile:[self fullDocumentsPathForTheFile:@"imageToAnalyse.jpg"]];
size = [UIImageJPEGRepresentation(tmpImage, compressionLevel) length];
[tmpImage release];
//In the following line, the 0.001f decrement is choose just in order test the increase of the memory
//compressionLevel = compressionLevel - 0.001f;
NSLog(@"Compression: %f",compressionLevel);
} 有什么想法可以让我摆脱它,或者为什么会发生这种情况?谢谢
发布于 2010-04-17 04:26:17
至少,在每次循环过程中分配和释放图像是没有意义的。它不应该泄漏内存,但它是不必要的,所以移动alloc/init并将其释放出循环。
此外,UIImageJPEGRepresentation返回的数据是自动释放的,因此它会一直挂起,直到当前的释放池耗尽(当你回到主事件循环时)。考虑添加以下内容:
NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];在循环的顶部,以及
[p drain] 在最后。这样你就不会泄漏所有的中间内存。
最后,对最佳压缩设置进行线性搜索可能效率很低。改为执行二进制搜索。
https://stackoverflow.com/questions/2655769
复制相似问题