Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(iphone) UIImageView setImage: leaks?

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

like image 501
eugene Avatar asked Jan 10 '11 08:01

eugene


2 Answers

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
like image 54
Valera Avatar answered Oct 01 '22 01:10

Valera


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.

like image 40
EmptyStack Avatar answered Oct 01 '22 00:10

EmptyStack