ios 多線程載入圖片,並實現緩衝

來源:互聯網
上載者:User

轉載自http://blog.csdn.net/deep_explore/article/details/8144613

通過使用NSOperationQueue實現多線程載入圖片,並實現緩衝

建立類cc

#import <Foundation/Foundation.h>@interface CC : NSOperation{    NSString *url;    NSString *imageName;    UIImage *image;    UIImageView *delegate;}@property (nonatomic,retain) NSString *url;@property (nonatomic,retain)NSString *imageName;@property (nonatomic,retain)UIImage *image;@property (nonatomic,retain)UIImageView *delegate;-(void)main;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate;@end@implementation CC@synthesize url,delegate,image,imageName;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{    if (self = [super init]) {        self.url = [url retain];        self.imageName = imageName;        self.delegate = delegate;    }    return self;}-(void) main{    //    NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: self.imageName];    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]];    [data writeToFile:cachefile atomically:YES];        //    self.image = [UIImage imageWithData:data];    [self performSelectorOnMainThread:@selector(ok) withObject:nil waitUntilDone:NO];}-(void)ok{    [self.delegate setImage:self.image]}@end

然後在viewcontroller中調用

 

//成員對列的初始化  queue=[[NSOperationQueue alloc]init];//圖片資源都在數組裡面了arry=[NSArray arrayWithObjects:@"http://static2.dmcdn.net/static/video/451/838/44838154:jpeg_preview_small.jpg?20120509163826",          @"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705",          @"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018",          @"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749",          @"http://static2.dmcdn.net/static/video/340/086/44680043:jpeg_preview_small.jpg?20120509180118",          @"http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_small.jpg?20120412153140",          @"http://static2.dmcdn.net/static/video/771/577/44775177:jpeg_preview_small.jpg?20120509183230",          @"http://static2.dmcdn.net/static/video/810/508/44805018:jpeg_preview_small.jpg?20120508125339",          @"http://static2.dmcdn.net/static/video/152/008/44800251:jpeg_preview_small.jpg?20120508103336",          @"http://static2.dmcdn.net/static/video/694/741/35147496:jpeg_preview_small.jpg?20120508111445",          @"http://static2.dmcdn.net/static/video/988/667/44766889:jpeg_preview_small.jpg?20120508130425",          @"http://static2.dmcdn.net/static/video/282/467/44764282:jpeg_preview_small.jpg?20120507130637",          @"http://static2.dmcdn.net/static/video/754/657/44756457:jpeg_preview_small.jpg?20120507093012",          @"http://static2.dmcdn.net/static/video/831/107/44701138:jpeg_preview_small.jpg?20120506133917",          @"http://static2.dmcdn.net/static/video/411/057/44750114:jpeg_preview_small.jpg?20120507014914",          @"http://static2.dmcdn.net/static/video/894/547/44745498:jpeg_preview_small.jpg?20120509183004",          @"http://static2.dmcdn.net/static/video/082/947/44749280:jpeg_preview_small.jpg?20120507015022",          @"http://static2.dmcdn.net/static/video/833/347/44743338:jpeg_preview_small.jpg?20120509183004",          @"http://static2.dmcdn.net/static/video/683/666/44666386:jpeg_preview_small.jpg?20120505111208",nil];  在tableview中實現圖片的載入-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  NSString *identifity=[NSString  stringWithFormat:@"identify%d%d",indexPath.section,indexPath.row ];    UITableViewCell *cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifity];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifity];        cell.selectionStyle=UITableViewCellSelectionStyleNone; NSString *filename=[NSString stringWithFormat:@"%d",indexPath.row];        NSLog(@"filename=====%@",filename);        NSString *cachefile=[NSTemporaryDirectory() stringByAppendingPathComponent:filename];                NSLog(@"cachefile=======-----%@",cachefile);        UIImage *Image=[UIImage imageWithContentsOfFile:cachefile];        if (Image) {            cell.imageView.image=Image;            NSLog(@"有緩衝,");        }else{             NSLog(@"無緩衝,");            NSLog(@"arry===%@",[arry objectAtIndex:indexPath.row]);            CC *o = [[CC alloc] initWith:[[arry retain] objectAtIndex:indexPath.row] imageName:[NSString stringWithFormat:@"%d",indexPath.row] delegate:cell.imageView];            [queue addOperation:o];            cell.imageView.Image=[UIImage imageNamed:@"default.png"];         //記得arry要retain,要不然滑動時回記憶體崩潰[[arry retain] objectAtIndex:indexPath.row] 這樣寫正確}}

刪除圖片緩衝方法

#define K_LOCATION_IMG_COUNT 200+ (void)checkLocationImgAndRemove {    NSString *AppPath=[NSHomeDirectory() stringByAppendingString:@"/Documents/Images/PermanentStore/"];    NSFileManager *manager = [NSFileManager defaultManager];    if([manager fileExistsAtPath:AppPath]) {        NSArray *locationImgArray = [manager contentsOfDirectoryAtPath:AppPath error:nil];        if ([locationImgArray count] > [K_LOCATION_IMG_COUNT intValue]) {            [manager removeItemAtPath:AppPath error:nil];        }    }}@"/Documents/Images/PermanentStore/"自定,是做緩衝圖片的檔案夾路徑!

清除webview中的緩衝

http://stackoverflow.com/questions/2523435/how-to-clear-uiwebview-cache

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.