(iphone)UIImageView setImage:泄漏?

发布于 2024-10-11 19:28:23 字数 145 浏览 3 评论 0原文

我正在通过 [self setImage: newImage] 更改 UIImageview 的图像;

看起来每次我用 newImage 这样做时,先前的图像似乎都没有被释放。
替换 UIImageView 图像的正确方法是什么?

谢谢

i'm changing image of UIImageview by [self setImage: newImage];

Looks like every time I does that with newImage, prior image doesn't seem to be released.
What's the correct way to replace image of UIImageView?

Thank you

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

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

发布评论

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

评论(4

青朷 2024-10-18 19:28:23

是的,UIImageView setImage 确实泄漏了!
实际上,泄漏的是 CGImage,而不是 UIImage(如仪器“分配”所示)

我使用 BrutalUIImage 而不是 UIImage

@interface BrutalUIImageView : UIView {
    UIImage *image;
}

@property(nonatomic, retain) UIImage *image;

@end

@implementation BrutalUIImageView
@synthesize image;

- (void)setImage:(UIImage *)anImage {
    [image autorelease];
    image = [anImage retain];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [image drawInRect:rect];
}

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

@end

Yes, UIImageView setImage does leak!
Actually, leaks CGImage, not UIImage (as instrument "allocation" shows)

I use BrutalUIImage instead of UIImage

@interface BrutalUIImageView : UIView {
    UIImage *image;
}

@property(nonatomic, retain) UIImage *image;

@end

@implementation BrutalUIImageView
@synthesize image;

- (void)setImage:(UIImage *)anImage {
    [image autorelease];
    image = [anImage retain];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [image drawInRect:rect];
}

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

@end
守护在此方 2024-10-18 19:28:23

UIImageView setImage: 永远不会泄漏,除非您传递的图像没有被释放。

如果您将自动释放的图像分配给图像视图,您的代码不会泄漏,如下所示。

UIImage *newImage = [UIImage imageNamed:@"sampleImage"];
[yourImageView setImage:newImage];

但是,如果您将映像分配到某处,则必须手动释放它。

UIImageView setImage: never leaks, unless the image you are passing doesn't get released.

Your code wont leak, if your are assigning an autoreleased image to the image view, something like the following.

UIImage *newImage = [UIImage imageNamed:@"sampleImage"];
[yourImageView setImage:newImage];

But, if you are allocating the image somewhere, you have to release it manually.

时光沙漏 2024-10-18 19:28:23

你的 BrutalUIImageVIew 类真的很有趣,但是通过使用 UIImage“drawInRect:”方法绘制图像,我丢失了 PNG 文件的透明区域。

你知道如何绘制图像,保持PNG透明吗?
(当然,不使用 UIImageVIew 会在调用“setImage:”时泄漏 CGImage)

Your BrutalUIImageVIew class is really interesting, but by drawing the Image using UIImage "drawInRect:" method, i loss the transparent areas of my PNG file.

Do you know how to draw the image, keeping the PNG transparence ?
(Of course, not using UIImageVIew wich leaks the CGImage while calling "setImage:")

木格 2024-10-18 19:28:23

是的 UIImageView setImage 确实泄漏了!

如果您循环浏览一堆图像,

   [yourImageView setImage:[UIImage imageNamed:@"sampleImage.png"]];

您可以看到仪器上的内存使用量不断增加。
这似乎是某种缓存,因为之后
循环浏览所有图像内存使用量将趋于平稳。

正确的,或者至少是无泄漏的方法是:

   NSString *thePath = [[NSBundle mainBundle] pathForResource:@"sampleImage" ofType:@"png"];
   UIImage *newImage =  [[UIImage alloc] initWithContentsOfFile:thePath];
   [yourImageView setImage:newImage];

我在我的代码上验证了这一点,因为我的应用程序循环了很多
大图像文件。

Yes UIImageView setImage indeed leaks!

If you cycle through a bunch of images with

   [yourImageView setImage:[UIImage imageNamed:@"sampleImage.png"]];

you can see on instruments memory usage increasing.
This seems to be some kind of caching going around since after
cycling through all the images memory usage will go flat.

The correct, or at least, the non leaky way to do it is:

   NSString *thePath = [[NSBundle mainBundle] pathForResource:@"sampleImage" ofType:@"png"];
   UIImage *newImage =  [[UIImage alloc] initWithContentsOfFile:thePath];
   [yourImageView setImage:newImage];

I verified this on my code as my APP was cycling through a lot
of large image files.

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