自適應(yīng)網(wǎng)絡(luò)圖片SDAutoLayout

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);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容