iPhone - UIImage imageScaledToSize 内存问题
我做了研究并多次尝试释放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将调用包装在 NSAutoreleasePool 中,其中将汇集它们的结果。然后你可以在该池上调用[池排水],其内容将被释放,包括图像。
但请注意,您将无法使用 NSAutoreleasePool 范围之外的图像,因此您的代码可能需要如下所示:
更新:
如果上述仍然给您带来问题,请参阅解决方案我发布了这个问题。问题涉及将图像旋转 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:
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
.