自适应网络图片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];//这个是和上个界面是不一样的  上个界面是拆开写的,这个没有

}

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

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,726评论 2 7
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,148评论 1 6
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,426评论 0 3
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,775评论 1 9
  • 十月怀胎,有人觉得漫长,而我怎么感觉一瞬间的时间呢,一眨眼我已孕育生命6个月了!从初期的小心翼翼,不知所措到现在一...
    命里阅读 139评论 0 1