使用后释放UIImage

发布于 2024-09-24 10:39:21 字数 380 浏览 0 评论 0原文

我想学习分配 uiimage 并释放它以进行内存管理的正确方法。
当我使用以下代码时,它在释放时崩溃,
如果我不使用[成像器释放];
程序没有崩溃,但显示成像器泄漏。有什么解决办法吗?

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:s@"nameofimage" ofType:@"jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];

[imageView release];

[imager release];

I want to learn the right way to alloc an uiimage and release it for memory management.
When i use the following code it crashes when deallocating,
if i dont use [imager release];
program doesnt crash but it shows a leak for imager. Any ideas for solution?

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:s@"nameofimage" ofType:@"jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];

[imageView release];

[imager release];

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

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

发布评论

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

评论(1

很酷又爱笑 2024-10-01 10:39:21

进行内存管理的正确方法就像您所做的那样。

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"nameofimage" ofType:@"jpg"]]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];
[imager release];
[imageView release]; 

如果您首先发布 uiimage 而不是 uiimageview ,可能会起作用,

我在这段代码中没有发现任何错误。如果应用程序崩溃,请检查释放后是否在其他任何地方使用相同的对象引用。

The right way of doing memory management is as you have done.

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"nameofimage" ofType:@"jpg"]]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];
[imager release];
[imageView release]; 

May be it work if you first release the uiimage than uiimageview

I am not finding any mistake in this code. If the application crashes then check if you are using the same object reference anywhere else after it gets released.

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