iOS 网络图片大小自适应

基于SDWebImage实现:
__weak typeof(self)weakSelf = self;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:gM1.imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
CGSize size = image.size;
weakSelf.imageView.heightJT = RECTWIDTH*size.height/size.width;
}];


防止内存溢出:
1、添加#import "SDImageCache.h"头文件
在使用SDWebImage加载较多图片造成内存警告时,定期调用
[[SDImageCache sharedImageCache] setValue:nil forKey:@“memCache”];
2、在AppDelegate.m文件
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
// 取消正在下载的操作
[[SDImageCache sharedImageCache] cancelAll];
// 清除内存缓存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil]; [[SDImageCache sharedImageCache] clearMemory];
}


扩展:
https://blog.csdn.net/benyoulai5/article/details/50462586

扩展 AppDelegate.m 方法:
https://www.jianshu.com/p/14118d40a878
https://www.cnblogs.com/tongyuling/p/4648976.html

你可能感兴趣的:(iOS基础)