IOS UITableView 載入未知寬高圖片的解決方案,
在開發中遇到了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 歡迎加入