I created a sample application that where I have a basic fullscreensize UIImageView set as the background. This app is very simple. All it is is 2 pages. (I will post the code below). The NIB only as UIImageView. I have tried 2 different scenarios –
Is there a problem with releasing NIB files and images.
I have been experiencing some memory leaks.
1) Example 1 (BAD)- I load the image directly in to the NIB. When I push the view, memory goes up by 5MB. When I pop (using NavigationController) the View off of the top, memory only goes down by a 1.5MB. Dealloc is indeed called. I have no IBOutlets. So, to me this is either caching or retaining this image in memory somewhere.
2) Example 2 (GOOD)- Set an IBOutlet for the UIImage view and I put the image in the UIImageview as shown below(I know I could create the UIImageView programmatically, but I am testing memory here not my screen creation skills). This cleans up nicely after itself. It removes the image from memory and I can see in Instruments that memory goes down.
@interface Page3VC : UIViewController {
UIImageView*backimage;
}
@property(retain, nonatomic) IBOutlet UIImageView*backimage;
end
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
backimage.image = [self getImage:@"hipHome.png"];
}
-(void)viewDidDisappear:(BOOL)animated {
backimage.image = nil;
[super viewDidDisappear:animated];
}
-(UIImage*)getImage:(NSString*)imageName{
//return [UIImage imageNamed:imageName];
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
}
Loading images via the NIB can be problematic — it’s known to ‘leak’ due to caching behaviour, but this is by design — please see this question. This caching is useful for showing small images in UITables (high performance), but not good for loading a single huge image into your UI.
We need to see more of your code! How is
getImagedefined?If you’re using
UIImage imageNamed:anywhere in your own code, you should be aware that it caches loaded images in memory. Dealloc’ing such an image won’t actually unload the cached image in memory!For more info, please see:
Difference between [UIImage imageNamed…] and [UIImage imageWithData…]?