更改 UIImageView 上的视图属性时出现内存问题

发布于 2024-11-05 19:44:45 字数 1050 浏览 5 评论 0原文

我在弹出窗口 (iPad) 中使用 UITableView 从文档目录中选择图像。然后该图像显示在 UIImageView 中。这是我用来将新图像放入 UIImageView 的代码:

- (void)changeImage:(NSString *)imageFilename {

    NSLog(@"changeImage to: %@", imageFilename);    

    NSFileManager *fileMgr;
    fileMgr = [NSFileManager defaultManager];

    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];

    NSString *imagePath = [docsDir stringByAppendingPathComponent:imageFilename];

    UIImage *newImage = [UIImage imageWithContentsOfFile:imagePath];

    NSLog(@"Path for new image: %@", imagePath);

    self.imageView.image = newImage;
    [newImage release];

    [fileMgr release];

}

第一次总是工作正常,但每当我尝试从弹出窗口再次更改显示的图像时,我都会收到以下错误: 程序收到信号:“EXC_BAD_ACCESS”。

我已将问题缩小到这一行:

self.imageView.image = newImage;

我尝试在该行之前添加以下内容:

self.imageView.image = nil;

这没有什么区别。我不明白为什么这第一次会起作用,但下次不会重复同样的行为。

非常感谢任何帮助。

I am using a UITableView inside a popover (iPad) to select an image from the Documents Directory. This image is then displayed in a UIImageView. Here is the code I am using to put the new image into the UIImageView:

- (void)changeImage:(NSString *)imageFilename {

    NSLog(@"changeImage to: %@", imageFilename);    

    NSFileManager *fileMgr;
    fileMgr = [NSFileManager defaultManager];

    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];

    NSString *imagePath = [docsDir stringByAppendingPathComponent:imageFilename];

    UIImage *newImage = [UIImage imageWithContentsOfFile:imagePath];

    NSLog(@"Path for new image: %@", imagePath);

    self.imageView.image = newImage;
    [newImage release];

    [fileMgr release];

}

This always works fine the first time around, but whenever I try to change the displayed image again from the popover, I get the following error:
Program received signal: “EXC_BAD_ACCESS”.

I've narrowed the problem down to this line:

self.imageView.image = newImage;

I tried adding the following before that line:

self.imageView.image = nil;

It made no difference. I can't figure out why this works the first time but won't repeat the same behaviour next time around.

Would greatly appreciate any help.

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

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

发布评论

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

评论(3

伤痕我心 2024-11-12 19:44:45

顺便说一句,您也不应该释放 fileMgr,因为您不拥有它。文档说它始终是同一个实例(单例) - 它不会中断,因为通常会覆盖单例上的“release”而不执行任何操作。不过,它不一定是这样的,而且是错误的。

BTW you also shouldn't release fileMgr, because you don't own it. Docs say that it is always the same instance (a singleton) - it doesn't break since it is common to override "release" on singletons to do nothing. Still, it doesn't have to be this way and is wrong.

谜兔 2024-11-12 19:44:45

从代码中删除 [newImage release]; 就可以了!

您没有分配/保留/新建 newImage,因此您不必释放它。

它第一次起作用是因为 UIImage *newImage = [UIImage imageWithContentsOfFile:imagePath]; 返回了一个对当前运行循环有效的自动释放对象,但是您过度释放了它,并且当run-loop 尝试再次使用它,因此会出现 EXC_BAD_ACCESS 信号。

Remove [newImage release]; from your code and you should be fine!

You didn't alloc/retain/new the newImage so you don't have to release it.

It works the first time because UIImage *newImage = [UIImage imageWithContentsOfFile:imagePath]; returned you an autorelease'd object that is valid for the current run-loop but you are over releasing it, and when the run-loop tries to use it again and hence the EXC_BAD_ACCESS signal.

你与清晨阳光 2024-11-12 19:44:45

我对 Objective-C 没有深入的了解,但我认为你应该复制图片:

self.imageView.image = [newImage copy];

I doesn't have a deep understanding of Objective-C, but I think you should copy the image:

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