(iphone) UIImageView setImage: leaks?

前端 未结 4 1553
无人共我
无人共我 2020-12-03 15:52

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.
Wha

4条回答
  •  感动是毒
    2020-12-03 16:14

    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.

提交回复
热议问题