您的位置:首页 > 理论基础 > 计算机网络

第03天多线程网络:(05):SDWebImage简单说明和使用

2017-04-20 00:00 435 查看
#####一、SDWebImage简单说明和使用

// 最复杂的方法
/**
参数1  : sd_setImageWithURL :  图片URL的地址
参数2  : placeholderImage   :  占位图片
参数3  : options            :  策略
SDWebImageRetryFailed = 1 << 0, //失败后尝试重新下载
SDWebImageLowPriority = 1 << 1, //低优先级
SDWebImageCacheMemoryOnly = 1 << 2, //只使用内存缓存
SDWebImageProgressiveDownload = 1 << 3, //渐进式下载
SDWebImageRefreshCached = 1 << 4,   //刷新缓存
SDWebImageContinueInBackground = 1 << 5,    //后台下载
SDWebImageHandleCookies = 1 << 6,   //处理保存在NSHTTPCookieStore中的cookies
SDWebImageAllowInvalidSSLCertificates = 1 << 7,     //允许不信任的 SSL 证书
SDWebImageHighPriority = 1 << 8,    //高优先级(优先下载)
SDWebImageDelayPlaceholder = 1 << 9,    //延迟占位图片
SDWebImageTransformAnimatedImage = 1 << 10, //转换动画图像
SDWebImageAvoidAutoSetImage = 1 << 11   //手动设置图像
参数4  : progress           :  下载进度 (receivedSize 已经下载的数据大小 ,expectedSize 要下载图片的总大小)
参数5  : completed          :  下载完成的操作 (image 要下载的图片, error 错误信息,cacheType 缓存的类型,imageURL)
缓存的类型
SDImageCacheTypeNone,   //不使用 SDWebImage 缓存,从网络下载
SDImageCacheTypeDisk,   //使用磁盘缓存
SDImageCacheTypeMemory  //使用内存缓存

*/
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"图片URL"] placeholderImage:[UIImage imageNamed:@"占位图片"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"%f",1.0 * receivedSize / expectedSize);

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"%zd",cacheType);
}];


code
LYHAppModel

>>>.h
#import <Foundation/Foundation.h>

@interface LYHAppModel : NSObject

/** app的名称 */
@property(nonatomic, strong) NSString *name;

/** app的图标 */
@property(nonatomic, strong) NSString *icon;

/** app的下载量 */
@property(nonatomic, strong) NSString *download;

// 提供一个方法 用于 字典转模型
+ (instancetype)LYHAppModelWithDict:(NSDictionary *)dict;

@end
>>>.m
#import "LYHAppModel.h"

@implementation LYHAppModel
+ (instancetype)LYHAppModelWithDict:(NSDictionary *)dict
{
LYHAppModel *model = [[LYHAppModel alloc]init];

#pragma  字典转模型 几种方式
#pragma mark 1.一个一个转
//model.name = dict["name"];
#pragma mark 2.KVC
[model setValuesForKeysWithDictionary:dict];

return model;
}
@end

VC

#import "ViewController.h"
#import "LYHAppModel.h"
#import "UIImageView+WebCache.h"

@interface ViewController ()

/** 数组源 */
// 如何选择使用 可变还是不可变 是根据需求来说的
// 如果数据是固定的 那么就用 不可变的
// 如果数据是不固定 那么就用 可变的

@property(nonatomic, strong) NSArray *dataA;
/** 内存缓存 */
// 每下载完一张图片 要保存起来
@property(nonatomic, strong) NSMutableDictionary *dict_images;

/** 队列(用来下载任务) */
@property(nonatomic, strong)NSOperationQueue *queue;

/* 图片下载操作缓存 */
@property(nonatomic, strong) NSMutableDictionary *dict_operations;

@end

@implementation ViewController
#pragma mark 懒加载
- (NSMutableDictionary *)dict_operations
{
if (_dict_operations == nil) {
_dict_operations = [NSMutableDictionary dictionary];
}
return _dict_operations;
}

- (NSOperationQueue *)queue
{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc]init];
// 在程序里面 最好只开3~5个队列
// 设置队列并发数
_queue.maxConcurrentOperationCount = 5;
}
return  _queue;
}

- (NSMutableDictionary *)dict_images
{
if (_dict_images == nil) {
_dict_images = [NSMutableDictionary dictionary];
}
return _dict_images;
}

- (NSArray *)dataA
{
if (_dataA == nil) {

// 字典数组
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil]];
NSMutableArray *arrM = [NSMutableArray array];
for (NSDictionary *dict in arr) {
[arrM addObject:[LYHAppModel LYHAppModelWithDict:dict]];
}
_dataA = arrM;

}
return _dataA;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}

#pragma mark --- ---
#pragma mark tableViewDataScoure
// 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

// 多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataA.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
static NSString *ID = @"app";
// 2.设置cell数据
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 拿到该行cell的数据
LYHAppModel *model = [self.dataA objectAtIndex:indexPath.row];
// 设置数据

cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.download;

// 最复杂的方法
/**
参数1  : sd_setImageWithURL :  图片URL的地址
参数2  : placeholderImage   :  占位图片
参数3  : options            :  策略
SDWebImageRetryFailed = 1 << 0, //失败后尝试重新下载
SDWebImageLowPriority = 1 << 1, //低优先级
SDWebImageCacheMemoryOnly = 1 << 2, //只使用内存缓存
SDWebImageProgressiveDownload = 1 << 3, //渐进式下载
SDWebImageRefreshCached = 1 << 4,   //刷新缓存
SDWebImageContinueInBackground = 1 << 5,    //后台下载
SDWebImageHandleCookies = 1 << 6,   //处理保存在NSHTTPCookieStore中的cookies
SDWebImageAllowInvalidSSLCertificates = 1 << 7,     //允许不信任的 SSL 证书
SDWebImageHighPriority = 1 << 8,    //高优先级(优先下载)
SDWebImageDelayPlaceholder = 1 << 9,    //延迟占位图片
SDWebImageTransformAnimatedImage = 1 << 10, //转换动画图像
SDWebImageAvoidAutoSetImage = 1 << 11   //手动设置图像
参数4  : progress           :  下载进度 (receivedSize 已经下载的数据大小 ,expectedSize 要下载图片的总大小)
参数5  : completed          :  下载完成的操作 (image 要下载的图片, error 错误信息,cacheType 缓存的类型,imageURL)
缓存的类型
SDImageCacheTypeNone,   //不使用 SDWebImage 缓存,从网络下载
SDImageCacheTypeDisk,   //使用磁盘缓存
SDImageCacheTypeMemory  //使用内存缓存

*/
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"lyh165"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
NSLog(@"%f",1.0 * receivedSize / expectedSize);

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"%zd",cacheType);
}];

return cell;
}

// http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fbb.png (图1正确连接)
// http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fb.png (错误连接 正在使用)

/*
1.UI很不流畅 (因为放到主线程里面)  ---> 开启子线程下载图片
2.图片被重复下载  ---> 先把之前已经下载的图片保存起来(字典)
内存缓存 ---> 磁盘缓存(保存到沙盒里面)
3.图片不会刷新(解决)
4.图片重复下载(图片下载需要时间,当图片还未完全下载之前,又要重新显示该图片)
图片的下载操作 添加两到三次 --->[添加操作缓存]
5.数据错乱 --->(设置占位图片)
*/

#pragma 当收到内存警告的时候 进行内存清理
- (void)didReceiveMemoryWarning
{
[self.dict_images removeAllObjects];
[self.queue cancelAllOperations]; // 如果正在下载的话,那么就取消下载
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C 多线程