Here's the deal: I have twenty five view controllers in my storyboard, each one has a UIImageView that displays a PNG with a large file size.
It turns out that a UIImageView inside a nib will load their image using [UIImageView initWithImage:[UIImage imageNamed:]]
method (see the docs). The problem with using imageNamed:
is that when each view controllers is loaded, the image in its UIImageView is placed in the cache, and my app quickly starts getting memory warnings and crashes.
The way out is to use [UIImageView initWithImage:[UIImage imageWithContentsOfFile:]]
, which loads the image but doesn't cache it. So what I'm trying to do is use in my NIB file a subclass of UIImageView that calls imageWithContentsOfFile
. But I can't figure out how to grab the name of the image file that's set in the Attributes Inspector in Interface Builder.
UIImageViewNoCache.h
#import <UIKit/UIKit.h>
@interface UIImageViewNoCache : UIImageView
@end
UIImageViewNoCache.h
#import "UIImageViewNoCache.h"
@implementation UIImageViewNoCache
-(id)initWithImage:(UIImage *)image{
self = [super init];
if(self){
self.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], theNameOfTheImageInTheNib];]
}
}
@end
So, how can I get theNameOfTheImageInTheNib
?