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];//這個(gè)是和上個(gè)界面是不一樣的? 上個(gè)界面是拆開寫的,這個(gè)沒有
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{#pragma mark - 圖片自適應(yīng)單元格高度 此處不能用注冊(cè)單元格,只能這樣寫
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;
}
還有下面這個(gè)方法,下面有詳細(xì)的截圖
#import@implementation UIImage (ImgSize)
/*根據(jù)圖片url獲取網(wǎng)絡(luò)圖片尺寸*/
+ (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);
//以下是對(duì)手機(jī)32位、64位的處理(由網(wǎng)友評(píng)論區(qū)拿到的:小怪獸飼養(yǎng)猿)
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);
}



