iOS 项目中获取后台返回的网络图片的尺寸
2019-12-10 本文已影响0人
selice
iOS 项目中获取后台返回的网络图片的尺寸
http://hszb01-1258046001.file.myqcloud.com/image/20191210/1575946089831699.jpg【请勿用于别的渠道】
1、直接获取
HNNewUserFuliGoodsDDCarouselModel*Model = self.FuliBannerModel.d.carousel[0];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:Model.carousel_url]];
UIImage *image = [UIImage imageWithData:data];
Model.imgsize = image.size;
Model.imgH = image.size.height/2;
Model.imgW = image.size.width/2;
NSLog(@"图片的宽高---w = %f,h = %f",image.size.width,image.size.height);
打印:图片的宽高---w = 750.000000,h = 798.000000,size = <UIImage:0x282221830 anonymous {750, 798}>
2、需要用到第三方库SDWebImage[在用于tableviewcell中刷新可能存在第一次进入时获取不到问题]
UIImageView *view = [[UIImageView alloc] init];
view.contentMode = UIViewContentModeScaleAspectFit;
[view sd_setImageWithURL:[NSURL URLWithString:Model.carousel_url] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
Model.imgsize = image.size;
Model.imgH = image.size.height/2;
Model.imgW = image.size.width/2;
NSLog(@"图片的宽高---w = %f,h = %f,size = %@",image.size.width,image.size.height,image);
}];
打印:图片的宽高---w = 750.000000,h = 798.000000,size = <UIImage:0x2829f6370 anonymous {750, 798}>