在開發中遇到了UITableView列表 UITableViewCell裝載圖片但不知Image的寬高 問題。
在解決該問題的時候,首先想到的是異步加載圖片 采用第三方框架SDWebImage 實現對圖片異步下載和緩存
以下是我采用的方法幾個關鍵地方
1.計算UITableView的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *imgURL = self.electionPictureArray.count > indexPath.row ? self.electionPictureArray[indexPath.row] :nil; if (imgURL) {
//根據當前Row的ImageUrl作為Key獲取圖片緩存 UIImage *img = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: imgURL ]; if (!img) { img = [UIImage resizedImageWithName:@"childshow_placeholder"];; } CGFloat height = img.size.height *Main_Screen_Width/img.size.width;//Image寬度為屏幕寬度 ,計算寬高比求得對應的高度 NSLog(@"----------------return Height:%f",height); return height; } return 0; }
2.在UITableViewCell中實現圖片的下載,回調下載完成刷新頁面代理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ElectronicBookCell *cell = [ElectronicBookCell cellWithTableView:tableView]; cell.imageUrl = self.electionPictureArray.count > indexPath.row ? self.electionPictureArray[indexPath.row] :nil; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }
在cell中的setImageUrl中進行下載圖片
-(void) setImageUrl:(NSString *)imageUrl{ if (imageUrl) { _imageUrl = imageUrl; UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageUrl]; // 沒有緩存圖片 if (!cachedImage) { __weak typeof(self) target = self; // 利用 SDWebImage 框架提供的功能下載圖片 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { // 保存圖片 [[SDImageCache sharedImageCache] storeImage:image forKey:imageUrl toDisk:YES]; // 保存到磁盤 if (imageUrl == target.imageUrl) { [target configPreviewImageViewWithImage:image]; } if ([self.delegate respondsToSelector:@selector(reloadCellAtIndexPathWithUrl:)]) { [self.delegate reloadCellAtIndexPathWithUrl:imageUrl]; } }]; }else { [self configPreviewImageViewWithImage:cachedImage]; } } }
/** * 加載圖片成功后設置image's frame */ - (void)configPreviewImageViewWithImage:(UIImage *)image { _previewWidth = Main_Screen_Width; _previewHeight = image.size.height *Main_Screen_Width/image.size.width; CGRect rect = _previewImageView.frame; rect.size.width = _previewWidth;// image.size.width; rect.size.height = _previewHeight; _previewImageView.frame = rect; _previewImageView.image = image; [self resetLayoutByPreviewImageView]; }
3.在Controller中實現代理方法,
-(void)reloadCellAtIndexPathWithUrl:(NSString *)url{ if (url) { for (int i = 0; i< self.electionPictureArray.count; i++) {
//遍歷當前數據源中並找到ImageUrl NSString *imgURL = self.electionPictureArray.count >i ? self.electionPictureArray[i] :nil; if ([imgURL isEqualToString:url]) { //獲取當前可見的Cell NSIndexPaths NSArray *paths = self.tableView.indexPathsForVisibleRows;
//判斷回調的NSIndexPath 是否在可見中如果存在則刷新頁面 NSIndexPath *pathLoad = [NSIndexPath indexPathForItem:i inSection:0]; for (NSIndexPath *path in paths) { if (path && path == pathLoad ) { [self.tableView reloadData]; } } } } } }
IOS開發技術交流QQ群:491355147 歡迎加入