// AppDelegate.m 文件中
-?(BOOL)application:(UIApplication?*)application?didFinishLaunchingWithOptions:(NSDictionary?*)launchOptions
{
//?監(jiān)控網(wǎng)絡(luò)狀態(tài)
[[AFNetworkReachabilityManager?sharedManager]?startMonitoring];
}
//?以下代碼在需要監(jiān)聽網(wǎng)絡(luò)狀態(tài)的方法中使用
AFNetworkReachabilityManager?*mgr?=?[AFNetworkReachabilityManager?sharedManager];
if(mgr.isReachableViaWiFi)?????{//?在使用Wifi,?下載原圖
}else{//?其他祸挪,下載小圖
}
}
cell 完善版顯示圖片代碼
- setItem:(CustomItem *)item
{
_item?=?item;
//?占位圖片
UIImage?*placeholder?=?[UIImage?imageNamed:@"placeholderImage"];
//?從內(nèi)存\沙盒緩存中獲得原圖
UIImage?*originalImage?=?[[SDImageCache?sharedImageCache]?imageFromDiskCacheForKey:item.originalImage];
if(originalImage)?{//?如果內(nèi)存\沙盒緩存有原圖,那么就直接顯示原圖(不管現(xiàn)在是什么網(wǎng)絡(luò)狀態(tài))
[self.imageView?sd_setImageWithURL:[NSURL?URLWithString:item.originalImage]?placeholderImage:placeholder];
}else{//?內(nèi)存\沙盒緩存沒有原圖
AFNetworkReachabilityManager?*mgr?=?[AFNetworkReachabilityManager?sharedManager];
if(mgr.isReachableViaWiFi)?{//?在使用Wifi,?下載原圖
[self.imageView?sd_setImageWithURL:[NSURL?URLWithString:item.originalImage]?placeholderImage:placeholder];
}elseif(mgr.isReachableViaWWAN)?{//?在使用手機自帶網(wǎng)絡(luò)
//?????用戶的配置項假設(shè)利用NSUserDefaults存儲到了沙盒中
//????[[NSUserDefaults?standardUserDefaults]?setBool:NO?forKey:@"alwaysDownloadOriginalImage"];
//????[[NSUserDefaults?standardUserDefaults]?synchronize];
#warning?從沙盒中讀取用戶的配置項:在3G\4G環(huán)境是否仍然下載原圖
BOOL?alwaysDownloadOriginalImage?=?[[NSUserDefaults?standardUserDefaults]?boolForKey:@"alwaysDownloadOriginalImage"];
if(alwaysDownloadOriginalImage)?{//?下載原圖
[self.imageView?sd_setImageWithURL:[NSURL?URLWithString:item.originalImage]?placeholderImage:placeholder];
}else{//?下載小圖
[self.imageView?sd_setImageWithURL:[NSURL?URLWithString:item.thumbnailImage]?placeholderImage:placeholder];
}
}else{// 沒有網(wǎng)絡(luò)
UIImage *thumbnailImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:item.thumbnailImage];
if(thumbnailImage) {// 內(nèi)存\沙盒緩存中有小圖
[self.imageView sd_setImageWithURL:[NSURL URLWithString:item.thumbnailImage] placeholderImage:placeholder];
}else{
[self.imageView sd_setImageWithURL:nil placeholderImage:placeholder];
}
}
}
}