UIImageView *backgroundImage = nil;
...
[backgroundImage setImage:[[ImageCache sharedInstance] imageNamed:@"backgroundImage.png"]];
#import <UIKit/UIKit.h>
@interface LPImageCache : NSObject {
NSMutableDictionary *cache;
}
#pragma mark Instance methods
- (UIImage *) imageNamed:(NSString *)imageNamed;
#pragma mark -
#pragma mark Singleton methods
+ (LPImageCache *)sharedInstance;
#pragma mark -
@end
#import "LPImageCache.h"
static LPImageCache *sharedInstance = nil;
@implementation LPImageCache
#pragma mark -
#pragma mark 인스턴스 메서드
- (id) init {
self = [super init];
if (self != nil) {
cache = [NSMutableDictionary new];
}
return self;
}
- (UIImage *)imageNamed:(NSString *)imageNamed {
UIImage *image = [cache objectForKey:imageNamed];
if (image == nil) {
image = [UIImage imageNamed:imageNamed];
[cache setObject:image forKey:imageNamed];
}
return image;
}
#pragma mark -
#pragma mark 싱글톤 메서드들
+ (LPImageCache *)sharedInstance {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[ImageCache alloc] init];
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
// 릴리즈 할 수 없는 객체를 기록.
return UINT_MAX;
}
- (void)release {
}
- (id)autorelease {
return self;
}
@end
'프로그램 > iPhone' 카테고리의 다른 글
oauth로 twitter api 인증하고 api 사용하기 (0) | 2011.01.31 |
---|---|
팝업 닫기 용 커스텀 버튼 (0) | 2011.01.26 |
아이폰 앱 76개 소스코드, 강좌, 개발 팁 링크모음 (0) | 2011.01.24 |
MGTwitterEngine 을 이용한 트위터(Twitter) 로그인. (0) | 2011.01.21 |
UIWebView 와 Application (App) 간의 통신 (0) | 2011.01.21 |