公司本身做的是一款教育类的app,可是后台传入的url没有size或者size 比,借助SDWebImage实现图片按比例展示:
获取SDWebImage返回的image,尺寸存入对应的图片model里
代码如下:
[cell.ImageView sd_setImageWithURL:[NSURL URLWithString:imgUrlString] placeholderImage:[UIImage imageNamed:@"icon_stub"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
if (!CGSizeEqualToSize(model.imageSize, image.size)) {
model.imageSize = image.size;
//SDAutolayout 的一个分类方法
[tableView reloadDataWithExistedHeightCache];
}
}
}];
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 450;
}else
{
ImageItem *model = [self.itemArray objectAtIndex:indexPath.row];
if (!CGSizeEqualToSize(model.imageSize, CGSizeZero)) {
return tableView.width * model.imageSize.height /model.imageSize.width;
}
return 150;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 450;
}else
{
ImageItem *model = [self.itemArray objectAtIndex:indexPath.row];
if (!CGSizeEqualToSize(model.imageSize, CGSizeZero)) {
return tableView.width * model.imageSize.height /model.imageSize.width;
}
return 150;
}