IOS UITableView 加载未知宽高图片的解决方案

时间:2021-02-20 02:45:14

在开发中遇到了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 欢迎加入