2013-06-13 52 views
1

是否有將圖像從網上下載到設備緩存的方法? 在我的應用程序中,有一些包含圖像的視圖。在每個tableView中都有25-30張圖片。 問題是滾動時加載圖像,結果是在滾動圖像需要2-3秒重新加載...看起來不太好:) 此刻,我在每個單元格使用NSURLRequest 。將圖像插入設備的緩存 - iOS

NSURL* url = [NSURL URLWithString:article.articleImage]; 
NSURLRequest* request = [NSURLRequest requestWithURL:url]; 
[NSURLConnection sendAsynchronousRequest:request 
queue:[NSOperationQueue mainQueue] 
completionHandler:^(NSURLResponse * response, 
NSData * data, 
NSError * error) 
{ 
    if (!error) 
    { 
     UIImage* image = [[UIImage alloc] initWithData:data]; 
              MainImg.image = image; 
              article.articleImg=image; 
    } 

}]; 

我試過使用多線程,但應用程序的啓動需要很長時間。 想法?

+0

參見[這裏] [1]的問題。它解釋了將圖像保存到緩存的鋤頭。 [1]:http://stackoverflow.com/questions/9289672/how-to-store-images-in-cache – Durgaprasad

回答

0

嗨,你需要的UIImageView + AFNetworking.h類別的下載和使用AFNetworking u的排序

使用其- (void)setImageWithURL:(NSURL *)url方法效果般的魅力

0

您可以使用NSURLCache用於此目的。昨天我和這個工作,我可以把等級:

@interface MyURLCache : NSURLCache 
@property (nonatomic, assign) NSTimeInterval timeAvaiable; 
@end 

@implementation MyURLCache 

- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path 
{ 
    self = [super initWithMemoryCapacity:memoryCapacity diskCapacity:diskCapacity diskPath:path]; 
    if(self){ 
     _timeAvaiable = 5*60;//5 мин 
    } 
    return self; 
} 
- (id)init 
{ 
    self = [super init]; 
    if(self){ 
     _timeAvaiable = 5*60; 
    } 
    return self; 
} 

+ (NSCachedURLResponse*)addInfoDataToCachedResponce:(NSCachedURLResponse*)cachedResponse 
{ 
    NSMutableDictionary* newUserInfo = [@{@"LastUpdate" : @([[NSDate date] timeIntervalSince1970])} mutableCopy]; 
    if([cachedResponse userInfo]) 
     newUserInfo[@"UserInfo"] = [cachedResponse userInfo]; 

    return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] 
                data:[cachedResponse data] 
               userInfo:newUserInfo 
              storagePolicy:[cachedResponse storagePolicy]]; 
} 

+ (NSCachedURLResponse*)removeInfoDataFromCachedResponce:(NSCachedURLResponse*)cachedResponse 
{ 
    return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response] 
                data:[cachedResponse data] 
               userInfo:[cachedResponse userInfo][@"UserInfo"] 
              storagePolicy:[cachedResponse storagePolicy]]; 
} 

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request 
{ 
    NSCachedURLResponse* result = [super cachedResponseForRequest:request]; 
    NSDate* lastUpdate = [NSDate dateWithTimeIntervalSince1970:[result.userInfo[@"LastUpdate"] doubleValue]]; 
    BOOL expired = fabs([lastUpdate timeIntervalSinceNow]) > _timeAvaiable; 
    return expired? nil : [[self class] removeInfoDataFromCachedResponce:result]; 
} 

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request 
{ 
    [super storeCachedResponse:[[self class] addInfoDataToCachedResponce:cachedResponse] forRequest:request]; 
} 

@end 

而剛剛啓用緩存時加載的應用程序:

MyURLCache *URLCache = [[MyURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 
                diskCapacity:20 * 1024 * 1024 
                 diskPath:[SDURLCache defaultCachePath]]; 
[NSURLCache setSharedURLCache:URLCache]; 

對於每個請求需要設置的CachePolicy到NSURLRequestReturnCacheDataElseLoad。

或者只是使用AFNetworking :)

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
       placeholderImage:(UIImage *)placeholderImage 
         success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success 
         failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 
0

在我所有的應用程序我使用這個框架。使用非常簡單

https://github.com/rs/SDWebImage

代碼示例

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"] 

];

0

使用:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES); 
    NSString *savedImagePath = [NSString stringWithFormat:@"%@/%@",[paths objectAtIndex:0],[article.articleImage lastPathComponent]]; 
    UIImage *image = [UIImage imageNamed:savedImagePath]; 
    if(!image) 
    { 
     NSURL* url = [NSURL URLWithString:article.articleImage]; 
     NSURLRequest* request = [NSURLRequest requestWithURL:url]; 
     [NSURLConnection sendAsynchronousRequest:request 
     queue:[NSOperationQueue mainQueue] 
     completionHandler:^(NSURLResponse * response, 
     NSData * data, 
     NSError * error) 
     { 
     if (!error) 
     { 
      [data writeToFile:savedImagePath atomically:NO]; 
      UIImage* image = [UIImage imageNamed:savedImagePath]; 
      MainImg.image = image; 
      article.articleImg=image; 
     } 

    }]; 
    } 
else 
{ 
    MainImg.image = image; 
    article.articleImg=image; 
}