比较原始的方法:
方法二:
方法三:
#import <foundation/foundation.h> #import "stringutils.h" @interface imagemanager : nsobject { nsmutabledictionary *_imagedict; nsmutablearray *_imagearr; } @property(nonatomic, strong) nsstring *httpurl; @property(nonatomic, strong) nsmutabledictionary *imagedict; @property(nonatomic, assign) dispatch_queue_t networkqueue; + (imagemanager *) sharedinstance; - (void)asyncimage:(nsstring *)imagename imageview:(uiimageview *)imageview; //插队 - (void)asyncimageinsert:(nsstring *)imagename imageview:(uiimageview *)imageview insert:(bool)insert; //不要在下载之前的数据 - (void)asyncimagecleanold:(nsstring *)imagename imageview:(uiimageview *)imageview cleanold:(bool)cleanold; @end
实现文件:
// // imagemanager.m // myb-ios // // created by warrior gao on 13-6-5. // copyright (c) 2013年 51myb. all rights reserved. // #import "imagemanager.h" @interface imagemanager() @end @implementation imagemanager //缓存图片的最大数量 static int counter = 0; @synthesize imagedict = _imagedict; //singleton + (imagemanager *)sharedinstance { static id instance; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ instance = self.new; }); return instance; } - (id)init { if((self = [super init])) { self.networkqueue = dispatch_queue_create("com.warrior.network.image", nil); _imagedict = [[nsmutabledictionary alloc] init]; _imagearr = [[nsmutablearray alloc] init]; } return self; } - (nsstring *) filefullpath:(nsstring *)filename { nsstring *cachepath = [nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filefullpath = [nsstring stringwithformat:@"%@/%@",cachepath,filename]; return filefullpath; } //不要在下载之前的数据 - (void)asyncimagecleanold:(nsstring *)imagename imageview:(uiimageview *)imageview cleanold:(bool)cleanold { if(cleanold) { [_imagearr removeallobjects]; } [self asyncimage:imagename imageview:imageview]; } //插队,优先 - (void)asyncimageinsert:(nsstring *)imagename imageview:(uiimageview *)imageview insert:(bool)insert { if([stringutils isempty:imagename]){ return; } nsdata *data = [nsdata datawithcontentsoffile:[self filefullpath:[imagename stringbyreplacingoccurrencesofstring:@"/" withstring:@"-"]]]; if(data == nil){ [_imagedict setvalue:imageview forkey:imagename]; if(insert) { [_imagearr insertobject:imagename atindex:0]; } else { [_imagearr addobject:imagename]; } [self cacheimage]; } else { [imageview setimage:[uiimage imagewithdata:data]]; } } //正常,附加到后面 - (void)asyncimage:(nsstring *)imagename imageview:(uiimageview *)imageview { [self asyncimageinsert:imagename imageview:imageview insert:no]; } //异步缓存图片到本地,最多有两个线程 -(void)cacheimage { for (; counter < 2 && _imagearr.count > 0; counter++) { nsstring *imagename = nil; @synchronized(self){ imagename = [[_imagearr objectatindex:0] copy]; [_imagearr removeobjectatindex:0]; } if(imagename == nil) continue; dispatch_async(self.networkqueue, ^{ nslog(@"starting: %@", imagename); uiimage *avatarimage = nil; nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@"%@%@",self.httpurl, imagename]]; nsdata *responsedata = [nsdata datawithcontentsofurl:url]; if(responsedata.length > 0) { [responsedata writetofile:[self filefullpath:[imagename stringbyreplacingoccurrencesofstring:@"/" withstring:@"-"]] atomically:no]; avatarimage = [uiimage imagewithdata:responsedata]; nslog(@"finishing: %@", imagename); if (avatarimage) { dispatch_async(dispatch_get_main_queue(), ^{ uiimageview *imageview = [_imagedict objectforkey:imagename]; if(imageview != nil && avatarimage != nil){ [imageview setimage:avatarimage]; } [_imagedict removeobjectforkey:imagename]; [imagename release]; }); } } counter--; [self cacheimage]; }); } } @end
以上所述就是本文的全部内容 了,希望大家能够喜欢。
如对本文有疑问, 点击进行留言回复!!
IOS系统中使用new Date()报错valid Date
解决Android10读取不到/sdcard/、/storage/emulated/0/文件的问题
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
网友评论