캐시 URL 이미지 iPhone uitableview
-
20-09-2019 - |
문제
URL에서 UitableView의 셀로로드 된 이미지를 캐시하는 방법에 대한 자습서를 찾고 있습니다.
여기서 예를 찾았습니다
http://www.ericd.net/2009/05/iphone-caching-images-in-memory.html#top
그러나 코드는 불완전합니다. 나는 객관적인 C 초보자이므로 누락 된 조각을 채우는 것이 매우 어렵다는 것을 알았습니다.
해결책 2
좋은 작업 예제가 여기에서 발견되었습니다
http://ezekiel.vancouver.wsu.edu/~wayne/yellowjacket/yellowjacket.zip
다른 팁
다음은 NSCACHE를 사용한 간단한 ImageCache 구현입니다. Imagecache는 Singelton입니다.
imagecache.h
#import <Foundation/Foundation.h>
@interface ImageCache : NSObject
@property (nonatomic, retain) NSCache *imgCache;
#pragma mark - Methods
+ (ImageCache*)sharedImageCache;
//- (void) AddImage:(NSString *)imageURL: (UIImage *)image;
- (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image;
- (UIImage*) GetImage:(NSString *)imageURL;
- (BOOL) DoesExist:(NSString *)imageURL;
@end
imagecache.m
#import "ImageCache.h"
@implementation ImageCache
@synthesize imgCache;
#pragma mark - Methods
static ImageCache* sharedImageCache = nil;
+(ImageCache*)sharedImageCache
{
@synchronized([ImageCache class])
{
if (!sharedImageCache)
sharedImageCache= [[self alloc] init];
return sharedImageCache;
}
return nil;
}
+(id)alloc
{
@synchronized([ImageCache class])
{
NSAssert(sharedImageCache == nil, @"Attempted to allocate a second instance of a singleton.");
sharedImageCache = [super alloc];
return sharedImageCache;
}
return nil;
}
-(id)init
{
self = [super init];
if (self != nil)
{
imgCache = [[NSCache alloc] init];
}
return self;
}
// - (void) AddImage:(NSString *)imageURL: (UIImage *)image
- (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image
{
[imgCache setObject:image forKey:imageURL];
}
- (NSString*) GetImage:(NSString *)imageURL
{
return [imgCache objectForKey:imageURL];
}
- (BOOL) DoesExist:(NSString *)imageURL
{
if ([imgCache objectForKey:imageURL] == nil)
{
return false;
}
return true;
}
@end
예시
UIImage *image;
// 1. Check the image cache to see if the image already exists. If so, then use it. If not, then download it.
if ([[ImageCache sharedImageCache] DoesExist:imgUrl] == true)
{
image = [[ImageCache sharedImageCache] GetImage:imgUrl];
}
else
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imgUrl]];
image = [[UIImage alloc] initWithData:imageData];
// Add the image to the cache
//[[ImageCache sharedImageCache] AddImage:imgUrl :image];
[[ImageCache sharedImageCache] AddImage:imgUrl withImage:image];
}
내가 작업 한 앱에 대해 (개념과 Lane Roathe의 우수한 uiimageview+캐시 카테고리에서 가져온 코드와 함께) 이것을 썼습니다. 사용합니다 Asihttprequest 수업도 훌륭합니다. 예를 들어, 더 이상 필요하지 않은 경우 요청을 취소 할 수있게하거나, 더 정확한 UI 업데이트를 허용하기 위해 통지 userInfo를 사용하여 요청을 취소 할 수 있습니다. 그러나 내 목적에 잘 작동합니다.
@implementation ImageFetcher
#define MAX_CACHED_IMAGES 20
static NSMutableDictionary* cache = nil;
+ (void)asyncImageFetch:(UIImage**)anImagePtr withURL:(NSURL*)aUrl {
if(!cache) {
cache = [[NSMutableDictionary dictionaryWithCapacity:MAX_CACHED_IMAGES] retain];
}
UIImage* newImage = [cache objectForKey:aUrl.description];
if(!newImage) { // cache miss - doh!
ASIHTTPRequest *imageRequest = [ASIHTTPRequest requestWithURL:aUrl];
imageRequest.userInfo = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:anImagePtr] forKey:@"imagePtr"];
imageRequest.delegate = self;
[imageRequest setDidFinishSelector:@selector(didReceiveImage:)];
[imageRequest setDidFailSelector:@selector(didNotReceiveImage:)];
[imageRequest startAsynchronous];
}
else { // cache hit - good!
*anImagePtr = [newImage retain];
}
}
+ (void)didReceiveImage:(ASIHTTPRequest *)request {
NSLog(@"Image data received.");
UIImage **anImagePtr = [(NSValue*)[request.userInfo objectForKey:@"imagePtr"] pointerValue];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *newImage = [[UIImage imageWithData:[request responseData]] retain];
if(!newImage) {
NSLog(@"UIImageView: LoadImage Failed");
}
else {
*anImagePtr = newImage;
// check to see if we should flush existing cached items before adding this new item
if( [cache count] >= MAX_CACHED_IMAGES)
[cache removeAllObjects];
[cache setValue:newImage forKey:[request url].description];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"ImageDidLoad" object: self userInfo:request.userInfo];
}
[pool drain];
}
이 코드를 다음과 같이 호출합니다.
[ImageFetcher asyncImageFetch:&icon withURL:url];
또한 해당 UIIMAGE의 소유자에게 언제 REDISPLAY를 해야하는지 알리기 위해 더 나은 또는 더 나쁘게 알림을 사용하고 있습니다.이 경우 TableView 컨텍스트에 있습니다.
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(imageDidLoad:) name:@"ImageDidLoad" object:nil];
}
- (void)imageDidLoad:(NSNotification*)notif {
NSLog(@"Received icon load notification.");
// reload table view so that new image appears.. would be better if I could
// only reload the particular UIImageView that holds this image, oh well...
[self.tableView reloadData];
}
- (void)dealloc {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
// ...
}
당신은 또한 확인하고 싶을 수도 있습니다 hjcache. 그것은 모든 캐싱을 투명하게 수행하고 스크롤 성능이 중요한 UitableViewCells에서 사용하기에 적합한 uiimageview 호환 뷰 클래스와 함께 제공됩니다.