iPhone - UIImage imageScaledToSize 内存问题

发布于 2024-08-04 22:29:39 字数 894 浏览 1 评论 0原文

我做了研究并多次尝试释放 UIImage 内存但没有成功。我在互联网上看到另一篇帖子,其中其他人也遇到了同样的问题。每次调用 imageScaledToSize 时,ObjectAlloc 都会继续攀升。

在下面的代码中,我从资源目录中提取本地图像,并通过一些模糊调整其大小。 有人可以提供一些关于如何释放名为....scaledImage 和 labelImage 的 UIImage 内存的帮助。这是 iPhone Intruments 已显示构建 ObjectAlloc 的代码块。这段代码被 NSTimer 多次调用。

//Get local image from inside resource
NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
    NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
    UIImage * blurMe = [UIImage imageWithData:imageData];

//Resize and blur image
    UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
    UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
    imgView.image = labelImage;

I have done research and tried several times to release the UIImage memory and have been unsuccessful. I saw one other post on the internet where someone else was having this same issue. Everytime imageScaledToSize is called, the ObjectAlloc continues to climb.

In the following code I am pulling a local image from my resource directory and resizing it with some blur. Can someone provide some help on how to release the memory of the UIImages called....scaledImage and labelImage. This is the chunk of code where the iPhone Intruments has shown to have the ObjectAlloc build up. This chunk of code is called several times with an NSTimer.

//Get local image from inside resource
NSString * fileLocation = [[NSBundle mainBundle] pathForResource:imgMain ofType:@"jpg"];
    NSData * imageData = [NSData dataWithContentsOfFile:fileLocation];
    UIImage * blurMe = [UIImage imageWithData:imageData];

//Resize and blur image
    UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
    UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
    imgView.image = labelImage;

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

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

发布评论

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

评论(1

感情废物 2024-08-11 22:29:39

您可以将调用包装在 NSAutoreleasePool 中,其中将汇集它们的结果。然后你可以在该池上调用[池排水],其内容将被释放,包括图像。

但请注意,您将无法使用 NSAutoreleasePool 范围之外的图像,因此您的代码可能需要如下所示:

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

UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
UIImage * imageCopy = [[UIImage alloc] initWithCGImage:labelImage.CGImage]; // Gives a non-autoreleased copy of labelImage

[pool drain]; // deallocates scaledImage and labelImage

imgView.image = imageCopy; // retains imageCopy

更新:

如果上述仍然给您带来问题,请参阅解决方案我发布了这个问题。问题涉及将图像旋转 90 度而不是缩放它,但前提是相同的(只是矩阵变换不同)。使用我发布的答案中的代码应该可以让您更好地控制内存管理,并避免使用未记录的 API,例如 _imageScaledToSize

You can wrap the calls in an NSAutoreleasePool, where their results will be pooled. Then you can call [pool drain] on that pool and its contents will be released, including the images.

Note however that you will not be able to use the images outside of the NSAutoreleasePool's scope, so you code might want to look something like:

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

UIImage * scaledImage = [blurMe _imageScaledToSize:CGSizeMake(blurMe.size.width / dblBlurLevel, blurMe.size.width / dblBlurLevel) interpolationQuality:3.0];
UIImage * labelImage = [scaledImage _imageScaledToSize:blurMe.size interpolationQuality:3.0];
UIImage * imageCopy = [[UIImage alloc] initWithCGImage:labelImage.CGImage]; // Gives a non-autoreleased copy of labelImage

[pool drain]; // deallocates scaledImage and labelImage

imgView.image = imageCopy; // retains imageCopy

UPDATE:

If the above is still giving you problems, please see the solution I posted to this question. The question involves rotating an image 90 degrees instead of scaling it, but the premise is the same (it's just the matrix transformation that is different). Using code like in the answer I posted should give you greater control over your memory management and steer you away from using undocumented APIs like _imageScaledToSize.

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