Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single ANIMATED UIImageView background throughout app

Tags:

ios

iphone

ios6

I've been reading everything I can find on here about this topic but am still not sure the best way to proceed. I have a heavy UIImageView that uses an array of fat UIImages acting as an animated loop. This UIImageView is serving as the background for every screen in the app. (Client's request, not mine, so don't hate.) We've optimized the png files as small as they can go but it's still a pretty heavy load.

I've read several posts about how UIImage searches the cache for an existing image of that name (Ex. Shared UIImageView for background image throughout app - Singleton Property) but this doesn't seem to be happening. I've also been reading here about singletons and instantiating in the appdelegate but I'm not sure if either of these are the right way to proceed.

What's the best way to load the UIImageView once over the life of the app and use it in the background of every viewcontroller? Btw, because it takes several seconds to load, I'm going to be adding a "loading" page at app start that uses a single static image and an activity indicator.

Also, I'm not as familiar with testing and performance tools. Which one should I be using to test performance and make sure this is what is causing the hesitations throughout the app?

Apologies in advance for the noob questions - I generally avoid asking questions at all but sometimes, as in this case, I don't even know where to begin the research.

like image 238
Justin Whitney Avatar asked Aug 08 '26 10:08

Justin Whitney


1 Answers

I'm rather pleased with myself. I went with the AppDelegate technique and it's working beautifully. I declared a UIImageView property in the AppDelegate, then add the image code to its getter:

- (UIImageView *)backgroundImageView {
    if (!_backgroundImageView) {
        NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:150];
        for (int imageNum = 0; imageNum < 150; imageNum++) {
            [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"image_00%03i.png",imageNum]]];
        }
        _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage animatedImageWithImages:tempArray duration:4.0]];
    }
    return _backgroundImageView;
}

This also allowed me to kill the object in applicationDidReceiveMemoryWarning.

Then I created a helper class that I can pop into all of my UIViewController classes:

+ (void)placeBackgroundImageUnderView:(UIView *)masterView {
    myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
    //Correct for shrinkage
    appDelegate.backgroundImageView.frame = masterView.frame;
    [masterView addSubview:appDelegate.backgroundImageView];
    [masterView sendSubviewToBack:appDelegate.backgroundImageView];
}

This allowed me to add a single line in each viewDidLoad method:

[HelperClass placeBackgroundImageUnderView:self.view];

The thing is, just using UIImage alone should cache the whole thing and speed up load time. But it seems that every now and then the images would have to reload - memory issues? So this allows me more control over it by instantiating the object once and using that same object, while also being to set that object to nil to free up memory if needed.

Since adding it, the whole app has been loading much faster. I added an additional load screen with an activity indicator for that initial load (also works beautifully) and everything after that is instant happy.

like image 190
Justin Whitney Avatar answered Aug 09 '26 22:08

Justin Whitney