根据用户的网络环境判断是下载原图还是缩略图(UIImageVie
2016-04-17 本文已影响117人
yyc223
这里用到AFN框架和SDWebImage框架
首先需要AFN监控网络状态
在AppDelegate.m中
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
创建一个UIImageView的Category
封装各种判断网络的状态的代码,以及缓存问题
可以防止tableViewCell的循环利用导致出现的各种问题
贴代码:
#import <UIKit/UIKit.h>
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
@interface UIImageView (YYDownload)
/**
* 设置imageView显示的图片
*
* @param originalImageURL 原图URL
* @param thumbnailImageURL 缩略图URL
* @param placeholderImage 占位图片
* @param completedBlock 获取图片完毕之后会调用的block
*/
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completedBlock;
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completedBlock;
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL ;
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage;
@end
#import "UIImageView+YYDownload.h"
@implementation UIImageView (YYDownload)
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completedBlock
{
[self downloadOriginalImageURL:OriginalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:completedBlock];
}
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL
{
[self downloadOriginalImageURL:OriginalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:nil];
}
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage
{
[self downloadOriginalImageURL:OriginalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:placeholderImage completed:nil];
}
/*
sd_setImageWithURL:placeholderImage:方法的执行步骤
1.取消当前imageView之前关联的请求
2.设置占位图片到当前imageView上面
3.如果缓存中有对应的图片,那么就显示到当前imageView上面
4.如果缓存中没有对应的图片,发送请求给服务器下载图片
*/
- (void)downloadOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completedBlock
{
UIImage *originalImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:OriginalImageURL];
if (originalImage) {//如果有,sd_setImageWithURL:内部会取消上次操作的请求,
[self sd_setImageWithURL:[NSURL URLWithString:OriginalImageURL] completed:completedBlock];
}else {//没有原图
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
if (manager.isReachableViaWiFi) {//wifi情况下,下载原图
[self sd_setImageWithURL:[NSURL URLWithString:OriginalImageURL] placeholderImage:placeholderImage completed:completedBlock];
}else if (manager.isReachableViaWWAN){
// 在使用手机自带网络
// 用户的配置项假设利用NSUserDefaults存储到了沙盒中
// [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"alwaysDownloadOriginalImage"];
// [[NSUserDefaults standardUserDefaults] synchronize];
//考虑用户的设置,如果仍然下载原图
#warning 从沙盒中读取用户的配置项:在3G\4G环境是否仍然下载原图
//手机网络下,下载缩略图
BOOL alwaysDownloadOriginalImage = [NSUserDefaults standardUserDefaults];
if (alwaysDownloadOriginalImage) {
//用户设置的YES下载原图
[self sd_setImageWithURL:[NSURL URLWithString:OriginalImageURL]placeholderImage:placeholderImage completed:completedBlock];
}else {
//下载小图
[self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImage completed:completedBlock];
}
}else{//没有网络,(没有原图)
//从缓存/沙盒中看有没有小图
UIImage *thumbnailImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:thumbnailImageURL];
if (thumbnailImage){
//如果缓存/沙盒中有小图
[self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] completed:completedBlock];
}else{//没有设置为空
[self sd_setImageWithURL:nil placeholderImage:placeholderImage completed:completedBlock];
}
}
}
}
@end