iOS UIImage DownLoad圖片的下載緩存全部在此
iOS圖片的下載緩存全部在此 分類: iOS編程 2012-11-03 21:15 2075人閱讀 評論(2) 收藏 舉報 注意: 我的文章只寫給自己看 ---------------------------------------------------------------------------------------- (一)這部分(感覺out了), 但是還是保留, 算是學習的痕跡. ---------------------------------------------------------------------------------------- (1)最簡單的下載,顯示圖片的方法: [plain] view plaincopy UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]]; imageView.image = [self loadImageFromUrl:@"http://storage.live.com/items/72A00BF5A838647C!1616?filename=meinv004.jpg"]; [self.view addSubview:imageView]; -(UIImage*)loadImageFromUrl: (NSString*)url { NSURL *imageUrl = [NSURL URLWithString:url]; NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; UIImage *image = [UIImage imageWithData:imageData]; return image; } 這種最簡單的圖片加載方式阻塞了main線程. 使得流程不能流暢進行. (2)開辟線程來解決這個問題. [plain] view plaincopy // set imageview UIImageView *imageView = [[UIImageView alloc] initWithFrame:[self.view bounds]]; imageView.backgroundColor = [UIColor yellowColor]; imageView.tag = imageView_tag; [self.view addSubview:imageView]; // load image in background NSString *url = IMAGE_URL; [self performSelectorInBackground:@selector(loadImageFromUrl:) withObject:url]; -(void)loadImageFromUrl: (NSString*)url { NSURL *imageUrl = [NSURL URLWithString:url]; NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; [self performSelectorOnMainThread:@selector(updateImageView:) withObject:imageData waitUntilDone:NO]; } -(void) updateImageView:(NSData*) data { UIImageView *imageView = (UIImageView *)[self.view viewWithTag:imageView_tag]; imageView.image = [UIImage imageWithData:data]; } 并且只能在main線程中設置UI的內容, 所以代碼量增加了較多. 代碼量暫且不管, 這里還有一個比較嚴重的問題就是每次都要加載圖片, 使用SDWebImage: [plain] view plaincopy #import <SDWebImage/UIImageView+WebCache.h> [imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; SDWebImage可以實現: *下載和緩存圖片. *相同的url不會被重復下載. *壞的url不會一直請求. 使用HJCache: [plain] view plaincopy // 目前HJCache不支持ARC, 所以這是個問題. ----------------------------------------------------------------------------------------------------------------- (二)多線程初步實現TableView的圖片顯示(之前用第三庫老是不穩定) 這個算是比較滿意的. ------------------------------------------------------------------------------------------------------------------------ [plain] view plaincopy @interface c:NSOperation @property NSString *url; @property NSString *imageName; @property UIImage *image; @property UIImageView *delegate; -(void) main; -(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate; @end @implementation c:NSOperation @synthesize url = _url,imageName=_imageName, image=_image, delegate=_delegate; -(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{ if (self = [super init]) { self.url = url; 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(u) withObject:nil waitUntilDone:NO]; } -(void)u{ [self.delegate setImage:self.image]; } [plain] view plaincopy queue = [[NSOperationQueue alloc] init];//這是成員隊列的實例化 設置TableView cell中的圖片: [plain] view plaincopy NSString *filename = [NSString stringWithFormat:@"%d", indexPath.row]; NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: filename]; UIImage *image = [UIImage imageWithContentsOfFile:cachefile]; if (image) { cell.imageView.image = image; } else { c *o = [[c alloc] initWith:[_objects objectAtIndex:indexPath.row] imageName:[NSString stringWithFormat:@"%d",indexPath.row] delegate:cell.imageView]; [queue addOperation:o]; cell.imageView.image= [UIImage imageNamed:@"placeholder.png"]; } 注: 保存一下測試圖片 urls
http://blog.csdn.net/deep_explore/article/details/8144613
原網址
posted on 2013-12-10 17:18 ACM_Someone like you 閱讀(923) 評論(0) 收藏 舉報
浙公網安備 33010602011771號