您的位置:首页 > 其它

异步加载图片-SDWebImage

2016-04-19 17:11 274 查看

异步加载图片-SDWebImage

SDWebImage缓存原理



1.UIImageView 加载

//导入<UIImageView+WebCache.h>
#import <UIImageView+WebCache.h>

//placeholderImage加载过程中出现的图片
[_homeImageView sd_setImageWithURL:URL(source.imgUrl) placeholderImage:nil];


2.异步加载获取网络图片

建议使用这个方法的时候单独创建一个公用的方法(例如Block)


// 方法1:只下载图片,不管理图片,不会缓存图片到内存和本地
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// 下载进度block
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
// 下载完成block
}];

// 方法2:既下载图片,又管理图片。其实方法2内部也是调用的方法1。只不过方法2会缓存图片,并且管理下载队列。
[[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// 下载进度block
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// 下载完成block
}];


我的使用方法:

//定义Block块
typedef void(^ImgBlockType)(UIImage *image);

/**
*  @param urlString 网络请求的图片Url
*/
+ (void)imageUrl:(NSString *)urlString success:(ImgBlockType)imgBlock {

[[SDWebImageManager sharedManager] downloadImageWithURL:URL(urlString) options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
imgBlock(image);
}];
}


更多关于处理图片:

http://www.wugaojun.com/blog/2015/06/20/sdwebimagezhong-na-xie-hao-yong-de-fang-fa/

3.加载Gif

1.加载本地Gif

//加载本地Gif
NSString  *name = @"smallCow.gif";
NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:name ofType:nil];
NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
loadingImageView.backgroundColor = [UIColor clearColor];

//方法1
loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];

//方法2
[loadingImageView sd_setImageWithURL:[NSURL fileURLWithPath:filePath]];

[self.view addSubview: loadingImageView];


2.加载网络Gif

[loadingImageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.XXXX.gif"]];


注:推荐大家一个好的可以看模拟器盒子的好工具SimPholders2, 大家可以去下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: