7

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

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
eugene
  • 39,839
  • 68
  • 255
  • 489

4 Answers4

9

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
Valera
  • 91
  • 1
6

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.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • 3
    i'm doing essentially samething as what your code does. But "instruments" shows memory increasing everytime i do that. tabbing out of "call trees" to "statistics" in "instruments allocations" and back to "call trees" shows memory goes back to where it should be. maybe autoreleased object is not properly displayed in time in instruments? – eugene Jan 10 '11 at 14:08
  • May be.. I'm not sure though.. Can you post the code how you are creating and assigning image? – EmptyStack Jan 11 '11 at 03:39
  • [UIImage imageNamed:] will cache image data, so that may be what's causing a "leak". – Bjorn Roche Oct 09 '15 at 16:16
0

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.

Cortex
  • 2,300
  • 2
  • 15
  • 14
0

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:")

Enerol
  • 1