保留使用 UIGraphicsGetImageFromCurrentImageContext 创建的 UIImage 的所有权
我使用此代码在不同时间获取窗口的屏幕截图,并将创建的 UIImage 放入一个数组中,该数组传递给另一个 UIViewController,以便它们可以全部显示在网格中。我尝试释放 UIImage 并且内存使用量永远不会下降...我如何在此处使用该图像一次,并保留所有权,以便在显示后我可以释放内存
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imagesArray addObject:图像];
[图片发布];
I am using this code to get a screenshots of the window at various different times and putting the UIImage created into an array which is passed on to another UIViewController so that they can be all displayed back in a grid. I try to release the UIImage and the memory usage never goes down ... how can I use the image here once, and retain ownership so I can release the memory once it is displayed
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imagesArray addObject:image];
[image release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIGraphicsGetImageFromCurrentImageContext()
返回“包含当前位图图形上下文内容的自动释放图像对象”。当您将图像添加到 imagesArray 时,
NSMutableArray
会对它进行保留,并且这就是你所需要的。当图像从数组中删除时,内存将被释放。
您不应该调用图像的发布。
UIGraphicsGetImageFromCurrentImageContext()
returns "an autoreleased image object containing the contents of the current bitmap graphics context."When you add the image to imagesArray,
NSMutableArray
does a retain on it, andit's all you need. The memory will be released when the image is removed from the array.
You should not call release on image.
我在 UIGraphicsGetImageFromCurrentImageContext() 上发生了内存崩溃...如果您要创建并释放很多它们,则应该在每次迭代时将它们包装在一个新的 AutoReleasePool 中。即使允许 NSRunLoop 勾选,Apple/iOS 也不足以对周围的垃圾进行清理。
例如
I had a memory crash on UIGraphicsGetImageFromCurrentImageContext() ... if you're creating and releasing a lot of them, you should wrap them in a fresh AutoReleasePool for each iteration. Even allowing the NSRunLoop to tick WAS NOT ENOUGH for Apple/iOS to do housekeeping on garbage lying around from this.
e.g.
我在后台线程中使用来自 UIGraphicsGetImageFromCurrentImageContext() 的保留 UIImage 图像出现奇怪的 UIImage 内存泄漏。问题是你应该只从应用程序的主线程调用这个函数。提防。
I was getting a strange UIImage memory leak using a retained UIImage image from UIGraphicsGetImageFromCurrentImageContext() in a background thread. The problem turned out to be you should only call this function from the main thread of your application. Beware.