pod 'SDAutoLayout'
pod 'SDWebImage'
#import "UIImageView+WebCache.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIView+SDAutoLayout.h"
主要代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self cellHeightForIndexPath:indexPath cellContentViewWidth:kSWidth tableView:tableView];//这个是和上个界面是不一样的 上个界面是拆开写的,这个没有
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{#pragma mark - 图片自适应单元格高度 此处不能用注册单元格,只能这样写
TwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TwoTableViewCell"];
if(!cell)
{
cell = [[TwoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TwoTableViewCell"];
}
[cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:arrayMut[indexPath.row]]];
CGSize size = [UIImage getImageSizeWithURL:[NSURL URLWithString:arrayMut[indexPath.row]]];
cell.imageViewIcon.sd_layout
.leftSpaceToView(cell.contentView, 0)//距左边的距离
.rightSpaceToView(cell.contentView, 0)//距右边的距离
.topSpaceToView(cell.contentView, 0)//局上边的距离
.autoHeightRatio(size.height/size.width);
[cell setupAutoHeightWithBottomView:cell.imageViewIcon bottomMargin:0];//bottom离下边线的距离
return cell;
}
还有下面这个方法,下面有详细的截图
#import@implementation UIImage (ImgSize)
/*根据图片url获取网络图片尺寸*/
+ (CGSize)getImageSizeWithURL:(id)URL{
NSURL * url = nil;
if ([URL isKindOfClass:[NSURL class]]) {
url = URL;
}
if ([URL isKindOfClass:[NSString class]]) {
url = [NSURL URLWithString:URL];
}
if (!URL) {
return CGSizeZero;
}
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGFloat width = 0, height = 0;
if (imageSourceRef) {
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
//以下是对手机32位、64位的处理(由网友评论区拿到的:小怪兽饲养猿)
if (imageProperties != NULL) {
CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
#if defined(__LP64__) && __LP64__
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
}
#else
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
}
#endif
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}