user3642382
user3642382

Reputation:

How to release a Image?

I want to release pictures until they'll be loaded again. I load them with this code:

_backgroundImage1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", random() % 5]];

This is how I change the alpha

if (backCount == 100) {
        if (currentBack ==YES) {

        _backgroundImage1.alpha = _backgroundImage1.alpha - 0.01;
        _backgroundImage2.alpha = _backgroundImage2.alpha + 0.01;

        if (_backgroundImage2.alpha >= 1) {
            backCount = 0;
            currentBack = NO;
            NSString*path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%u", arc4random() % 4] ofType:@"jpg"];
            UIImage *Image = [UIImage imageWithContentsOfFile:path];
            _backgroundImage1.image = Image;

            //_backgroundImage1.image = nil;
            //_backgroundImage1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", random() % 5]];

        }
    } else {

Upvotes: 0

Views: 362

Answers (3)

Rob
Rob

Reputation: 437432

The imageNamed caches images. If you don't want it to be cached, use imageWithContentsOfFile.

As the imageNamed documentation warns us:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

Thus, you would:

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"]; 

_backgroundImage1.image = [UIImage imageWithContentsOfFile:path];

You've subsequently said that you're looking for "an animation that fades over from one random picture to an other". In that case, every time you change the image, you might just want to do:

// fade from current image to new image

[UIView transitionWithView:imageView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%ld", random() % 5] ofType:@"jpg"];
    imageView.image = [UIImage imageWithContentsOfFile:path];
} completion:^(BOOL finished) {
    // do whatever you want at the end
}];

Upvotes: 4

Sean Kladek
Sean Kladek

Reputation: 4446

imageNamed: caches images locally. From the docs:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

If you want it to load the image from disk each time, use imageWithContentsOfFile:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourImageName" ofType:@"yourImageFileType"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

Upvotes: 0

Hermann Klecker
Hermann Klecker

Reputation: 14068

With ARC:

_backgroundImage1=nil;

assuming that -backgroundImage1 is the only (strong) reference that is kept to the image.

Without ARC

[_backgroundImage1.image release];

Well, the this could be dangerous assuming that _backgroundImage1 is an instance of UIImageView or whatever that still wants to access its property image. So

_backgroundImage1=nil;

would be a good idea here too.

Upvotes: 0

Related Questions