版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337814
我们在开发中一般下载图片会使用SDWebImage这个第三方,可在不同的网络下如果后台返回的图片有小图和大图且有:不同的网络下下载不同的图片的需求,我们需要做相应的判断:
AFNetworkReachabilityManager * manager = AFNetworkReachabilityManagersharedManager;
// 设置图片
UIImage * bigPic = [SDImageCachesharedImageCache imageFromDiskCacheForKey:_topicModel.image1];
UIApplication *app = UIApplicationsharedApplication;
NSArray *children = [[appvalueForKeyPath:@"statusBar"valueForKeyPath:@"foregroundView"]subviews];
int type =0;
for (id childin children) {
if (childisKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")) {
type = [child valueForKeyPath:@"dataNetworkType"intValue];
}
}
switch (type) {
case1:
NSLog(@"2G");
break;
case2:
NSLog(@"3G");
case3:
NSLog(@"4G");
case5:
NSLog(@"wifi");
default:
NSLog(@"无网络");
break;
}
if (bigPic) { // 如果有大图
[_picV sd_setImageWithURL:NSURL URLWithString:_topicModel.image1 placeholderImage:UIImage imageNamed:@""];
}else{ // 如果没有大图需要进行下载
// 需要判断当前网络的状态
if (manager.isReachableViaWiFi) { // 如果是Wi-Fi下载大图
[_picV sd_setImageWithURL:NSURL URLWithString:_topicModel.image1 placeholderImage:UIImage imageNamed:@""];
}else if (manager.isReachableViaWWAN){ // 如果是手机网络
// 如果是2G网络下载下图,如果是3G/4G网络下载大图
if (type == 1) {
[_picV sd_setImageWithURL:NSURL URLWithString:_topicModel.image0 placeholderImage:UIImage imageNamed:@""];
}else{
[_picV sd_setImageWithURL:NSURL URLWithString:_topicModel.image1 placeholderImage:UIImage imageNamed:@""];
}
}else{ // 没有网络
UIImage * littleImage = [SDImageCache sharedImageCache imageFromDiskCacheForKey:_topicModel.image0];
if (littleImage) {
// 如果有小图使用小图
[_picV sd_setImageWithURL:NSURL URLWithString:_topicModel.image0 placeholderImage:UIImage imageNamed:@""];
}else{
// 如果没有小图使用占位图
[_picV sd_setImageWithURL:nil placeholderImage:UIImage imageNamed:@""];
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有