释放通过 imageNamed 加载的保留 UIImage 属性?

发布于 2024-08-06 10:51:26 字数 317 浏览 8 评论 0原文

在我的类对象中,我为 UIImage 定义了一个 (nonatomic,retain) 属性。 我用通过加载的图像分配了此属性

[UIImage imageNamed:@"file.png"];

如果在某个时候我想将此属性重新分配给另一个图像,我是否必须释放先前的引用?

我很困惑,因为通过保留属性,我知道我应该释放它。 但是因为 imageNamed: 是一种方便的方法(不使用 alloc),所以我不确定这里应用什么规则。

感谢您的见解!

In my class object i've defined a (nonatomic, retain) property for UIImage.
I assigned this property with an image loaded via

[UIImage imageNamed:@"file.png"];

If at some point I want to reassign this property to another image, should I have to release the prior reference?

I am confused because by the retain property I know i should release it.
But because imageNamed: is a convenience method (does not use alloc), im not sure what rule to apply here.

Thanks for the insight!

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

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

发布评论

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

评论(5

故事灯 2024-08-13 10:51:26

正确,Florin...但是根据上面的讨论,如果一个人使用一个 setter 来(通过合成或手动)执行“保留”,那么就不需要额外的保留。

换句话说,以下内容是正确的(并且没有内存泄漏),恕我直言,我是对的吗?我认为这就是这个问题的初衷……我也想确定一下。 ;-) 谢谢!

@interface MyClass {
    UIImage *myImage;
}
@property (nonatomic, retain) UIImage *myImage;
@end

@implementation MyClass
@synthesize myImage;

- (void) someMethod {

    self.myImage = [UIImage imageNamed:@"foo.png"];
}

- (void) someOtherMethod {

    self.myImage = [UIImage imageNamed:@"bar.png"];
}

- (void) dealloc {

    self.myImage = nil;
    [super dealloc];
}
@end

Correct, Florin... but per the discussion above, if one is using a setter for the property that (either via synthesize, or manually) does the "retain", then there's no need for the extra retain.

In other words, the following would be correct (and free of memory-leaks), IMHO, am I right? I think this was the original intent of the question... and I'd also like to be certain. ;-) thx!

@interface MyClass {
    UIImage *myImage;
}
@property (nonatomic, retain) UIImage *myImage;
@end

@implementation MyClass
@synthesize myImage;

- (void) someMethod {

    self.myImage = [UIImage imageNamed:@"foo.png"];
}

- (void) someOtherMethod {

    self.myImage = [UIImage imageNamed:@"bar.png"];
}

- (void) dealloc {

    self.myImage = nil;
    [super dealloc];
}
@end
朮生 2024-08-13 10:51:26

图像将按照命名规则自动发布返回给您。通过 setter 将其分配给具有保留属性的属性将保留它。通过 setter 将另一个图像分配给属性将释放旧图像并保留新图像。

The image is returned to you autoreleased as per the naming rules. Assigning it to a property with a retain attribute via the setter will retain it. Assigning another image to the property via the setter will release the old image and retain the new one.

若无相欠,怎会相见 2024-08-13 10:51:26

当您使用 nonatomic & 定义属性时retain,它会为您创建一个如下所示的 setter:

-(void)setImage:(UIImage*)newImage {
  if (image != newImage) {
    [image release];
    image = [newImage retain];
  }
}

如您所见,它在保留新值之前释放以前的值。

在您的特定情况下,当您将 -[UIImage imageNamed:] 返回的自动释放图像分配给属性时,将自动保留该图像,然后在您分配另一个图像(或 nil) 到属性。

When you define a property with nonatomic & retain, it creates a setter for you that looks like this:

-(void)setImage:(UIImage*)newImage {
  if (image != newImage) {
    [image release];
    image = [newImage retain];
  }
}

As you can see, it releases the previous value before retaining the new value.

In your particular case, the autoreleased image returned by -[UIImage imageNamed:] will be automatically retained when you assign it to the property, and then automatically released when you assign another image (or nil) to the property.

青巷忧颜 2024-08-13 10:51:26

来自 文档

...如果您打算保留返回的图像对象,则必须像保留任何 Cocoa 对象一样保留它。

这意味着,如果您不想再保留它,则应该释放它(假设您已经保留了它)。

From the docs:

...if you plan to hold onto a returned image object, you must retain it like you would any Cocoa object.

The implication being that if you no longer want to hold onto it, you should release it (assuming you've retained it).

冷情 2024-08-13 10:51:26

您应该释放所有保留的对象,但是当您定义图像对象时,我相信您的代码应该如下所示:

UIImage *img = [[UIImage imageNamed:@"file.png"] retain];

You should release all the objects you're retaining, however when you define your image object, I believe your code should look like this:

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