为什么我的图像泄露或变成僵尸?

发布于 2024-12-10 12:48:41 字数 725 浏览 2 评论 0原文

我有一个可以切换图像的应用程序,但我遇到了很多麻烦...... 第一个“del”是de AppDelegate,“displayImage”在其.m中定义 “equiz”是一个整数,但所有图像都被命名​​为“1”、“2”等

所以当我使用此代码时,我会发生泄漏,但不会崩溃:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = del.displayImage.frame;
    del.displayImage = imgView;
    [imgView release];

然后我使用这个,但当我转到设备的“桌面”,然后返回应用程序:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    del.displayImage.image = img;
    [img release] //"that line"

然后您说:“您正在释放您不拥有的对象” 我说:“如果我删除“那行”,我会得到同样的泄漏 D:

有人可以帮我解决这个问题吗? 预先非常感谢您:D

I have an app that switches an image, but i get a lot of trouble...
first "del" is de AppDelegate, and "displayImage" is defined in its .m
"equiz" is an integer, but all the images are named "1", "2" and like that

So when i use this code i get leaks but no crash:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = del.displayImage.frame;
    del.displayImage = imgView;
    [imgView release];

Then i use this but i get a crash when i go to the "desktop" of the device, and return to the app:

    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d",equiz]];
    del.displayImage.image = img;
    [img release] //"that line"

Then you say: "you are releasing the object that you dont own"
And i say: "If i delete "that line" i'll get the same leak D:

Can someone help me with this issue?
Thank you very much in advance :D

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

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

发布评论

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

评论(1

泼猴你往哪里跑 2024-12-17 12:48:41

嗨,我也遇到了类似的问题,它来自 NSString。所以我的解决方案是这样的:
在 .h 文件中创建,

NSString *imageName;
@property(nonatomic, retain)NSString *imageName;

然后在 .m 文件中执行此操作

@synthesize imageName;
...

imageName = [NSString stringWithFormat:@"%d",equiz];
UIImage *img = [UIImage imageNamed:imageName];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = del.displayImage.frame;
    del.displayImage = imgView;
    [imgView release];

...

并在 dealloc 方法中释放 imageName...

- (void)dealloc {
   [imageName release];
   [super dealloc];
}

对我来说这是有效的..希望帮助也是您的解决方案。

Hi I was have similar problem and it was from NSString. So my solution was this:
in .h file create

NSString *imageName;
@property(nonatomic, retain)NSString *imageName;

then in .m file do this

@synthesize imageName;
...

imageName = [NSString stringWithFormat:@"%d",equiz];
UIImage *img = [UIImage imageNamed:imageName];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = del.displayImage.frame;
    del.displayImage = imgView;
    [imgView release];

...

and in dealloc method release imageName...

- (void)dealloc {
   [imageName release];
   [super dealloc];
}

By me this works .. Hope help is solution for you too.

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