UIImageJPEGRepresentation - 内存释放问题

发布于 2024-08-29 08:33:50 字数 798 浏览 4 评论 0原文

在iPhone应用程序上,我需要通过邮件发送最大尺寸为300Ko的jpg(我不知道mail.app可以有的最大尺寸,但这是另一个问题)。为此,我尝试降低质量,直到获得低于 300Ko 的图像。

为了获得给我300Ko以下的jpg的质量(压缩级别)的良好价值,我做了以下循环。 它正在工作,但是每次执行循环时,尽管有“[tmpImage release];”,内存还是会增加我的jpg(700Ko)原始大小的大小。

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);
} 

关于如何摆脱它或为什么会发生这种情况有什么想法吗? 谢谢

On a iPhone app, I need to send a jpg by mail with a maximum size of 300Ko (I don't no the maximum size mail.app can have, but it's another problem). To do that, I'm trying to decrease quality until obtain an image under 300Ko.

In order to obtain the good value of the quality (compressionLevel) who give me a jpg under 300Ko, I have made the following loop.
It's working, but each time time the loop is executed, the memory increase of the size of the original size of my jpg (700Ko) despite the "[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);
} 

Any ideas about how can i get it off, or why it happens?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-09-05 08:33:50

至少,在循环的每次行程中分配和释放图像是没有意义的。它不应该泄漏内存,但这是不必要的,因此将 alloc/init 和release移出循环。

此外, UIImageJPEGRepresentation 返回的数据是自动释放的,因此它会一直保留,直到当前释放池耗尽(当您返回主事件循环时)。考虑添加:

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

在循环的顶部和

[p drain] 

末尾。这样你就不会泄漏所有的中间内存。

最后,对最佳压缩设置进行线性搜索可能效率很低。改为进行二分搜索。

At the very least, there's no point in allocating and releasing the image on every trip through the loop. It shouldn't leak memory, but it's unnecessary, so move the alloc/init and release out of the loop.

Also, the data returned by UIImageJPEGRepresentation is auto-released, so it'll hang around until the current release pool drains (when you get back to the main event loop). Consider adding:

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

at the top of the loop, and

[p drain] 

at the end. That way you'll not be leaking all of the intermediate memory.

And finally, doing a linear search for the optimal compression setting is probably pretty inefficient. Do a binary search instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文