将网络图片缓存到本地减少加载时间
GiftCacheUtl.h
+(void)downloadOne:(const NSDictionary *)dicGift;
+(void) dowloadGif;
+(NSString*) getGiftCacheDir;
+(NSURL*) getCacheFileByUrl:( NSString*)giftUrl;
GiftCacheUtl.m
+(NSString*) getGiftCacheDir{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
+(NSURL*) getCacheFileByUrl:( NSString*)giftUrl{
if (giftUrl == nil || giftUrl.length == 0)
return [NSURL URLWithString:giftUrl];
NSString *fileName = [giftUrl lastPathComponent] ;
NSString *documentsDirectory = [self getGiftCacheDir];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
NSLog(@"getCacheFileByUrl file exits:%@",filePath);
NSURL *url = [[NSURL alloc] initFileURLWithPath: filePath];
return url;
}
NSLog(@"getCacheFileByUrl =>NO HIT:%@",filePath);
return [NSURL URLWithString:giftUrl];
}
+(void)downloadOne:(const NSDictionary *)dicGift{
NSString *urlToDownload = [dicGift objectForKey:@"swf"] ;
NSString *documentsDirectory = [self getGiftCacheDir];
NSString *fileName = [urlToDownload lastPathComponent] ;
//NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
NSLog(@"downloadOne file exits:%@",filePath);
return;
}
NSLog(@"downloadOne Started:%@",urlToDownload);
//NSString *urlToDownload = @"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved %@!",filePath);
});
}
}
+ (void)dowloadGif{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
NSLog(@"Beginning download");
NSDictionary *json = nil;
int nCount = 0;
BG_DOWNLOAD_GIT:{
nCount++;
NSString *stringURL = @"http://zb.broad2008.com/api/public/index.php?service=Home.getGiftList";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//to json
NSError *jsonError;
//NSData *objectData = [@"{\"2\":\"3\"}" dataUsingEncoding:NSUTF8StringEncoding];
NSData *objectData = urlData;
json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
}
int ret = [[json valueForKey:@"ret"] intValue];
if (ret != 200){
NSLog(@"downloadGift=> error, ret is !200 ");
}
NSDictionary* data = [json valueForKey:@"data"];
int code =[[data valueForKey:@"code"] intValue];
if ( code != 0 ){
NSLog(@"downloadGift=> error, code is !0 ");
}
NSArray *list = [data valueForKey:@"info"];
if(list == nil || list.count == 0 ){
NSLog(@"downloadGift=> error,info count is 0");
if(nCount > 10)return;
sleep(nCount*10);
goto BG_DOWNLOAD_GIT;
}
for (NSDictionary *dic in list) {
[self downloadOne: dic];
}
});
}
使用
app启动后下载
[GiftCacheUtl dowloadGif];
从本地沙盒获取图片
NSURL *giftUrl = [GiftCacheUtl getCacheFileByUrl:gift_url];