您的位置:首页 > 移动开发 > IOS开发

SDWebImage的使用

2015-09-06 16:33 543 查看
本文是对SDWebImage相关使用的翻译,原文请参考https://github.com/rs/SDWebImage

本库为UIImageView提供一个分类,并且支持来自web的远程图片加载

它提供:

1.添加web图片和缓存管理到Cocoa Touch 框架的UIImageView分类

2.异步图片下载

3.异步内存和硬盘图片缓存有效操作,并且有自动缓存

4.GIF动态支持

5.WebPage形式的支持

6.背景图片解压

7.保证相同的URL不多次下载

8.保证伪URLs不多次重试

9.保证主线程决不被堵塞

10.完美

11.使用GCD和ARC

12.支持Arm64

注意:3.0版本的SDWebImage不完全向后兼容2.0版本,并且要求最小iOS5.1.1部署版本。如果需要支持iOS<5.0,请使用最新的2.0版本

在UITableView内使用UIImageView+WebCache分类

导入UIImageView+WebCache.h头文件,并且在tableView:cellForRowAtIndexPath:
中调用sd_setImageWithURL:placeholderImage:方法。从异步下载到缓存管理,一切尽在你的掌控。

#import <SDWebImage/UIImageView+WebCache.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

cell.textLabel.text = @"My Text";
return cell;
}

使用blocks

使用blocks,无论图片检索是否成功你都能知晓image下载的进程

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {... completion code here ...}];

注意:无论成功或者失败,如果你的image在完成之前请求放弃,block都会被调用

使用SDWebImageManager

SDWebImageManager类在UIImageView+WebCache分类之后。它和带有图片缓存存储的异步下载有关。你能直接用于带有缓存的图片下载,不仅仅是在UIView中,还有其它正文中

SDWebImageManager的简单例子

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downlo
fe59
adImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
}
}];

独立使用异步图片下载

它也可能独立用于异步图片下载:

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
// do something with image
}
}];

独立使用异步图片缓存

它也可能独立地用于异步的基于图片缓存存储。SDImageCache维护内存缓存和可选的硬盘缓存。硬盘缓存写操作被异步执行,所以它不添加不必要的潜在因素到UI中

SDImageCache类提供单例便于使用,但如果你想创建分离的缓存命名空间,你能够创建你自己的实例。

检查缓存,你使用queryDiskCacheForKey:done:方法。如果方法返回nil,说明缓存目前没有自己的图片。因此你能够形成或者缓存它。缓存的关键是为图片缓存的应用的唯一标识符。它通常是图片的绝对URL

SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];

如果图片在内存中没有发现的话,默认SDImageCache将查看硬盘缓存,通过调用可选方法,你能阻止这方法发生

imageFromMemoryCacheForKey:

存储图片到缓存中,你能使用storeImage:forKey:方法:

[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

默认情况下,图片被存储到内存和硬盘缓存上(异步地),如果你仅想存储到内存缓存中,使用可选方法storeImage:forKey:toDisk并且第三个参数为负数

使用缓存key过滤器

有的时候你不想使用图片URL作为缓存key,因为部分URL是动态的(例如:为了访问可控目的)。SDWebImageManger提供一个方法设置key过滤器,输入NSURL并且输出缓存key字符串

下面的例子在应用代理中设置了过滤器,在使用它作为缓存key之前,将从URL中移除任何的查询字符串

SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) {
url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
return [url absoluteString];
};

// Your app init code...
return YES;
}

问题

UITableViewCell中使用动态图片的size

UITableView为一个cell决定图片的大小是按照第一个图片设置的大小。如果远程图片没有像你占位图片同样地size,你可能会遇到一个奇怪变形扩展的问题,下面方法文章会给出这个问题的而解决方法

http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/

图片更新操作

默认情况下,SDWebImage确实非常激进的缓存。它忽视各种缓存控制头被HTTP返回并且返回的图片缓存没有时间限制。这意味着你的图片URLs是静态的URLs,也就是说指向的图片绝不会改变。如果指向的图片发生改变,相应的部分URL应该改变。

如果你不控制你正在使用的图片服务器,当URL内容更新时你可能不能够改变URL。例如Facebook头像的例子。在这个例子中,你应该使用SDWebImageRefreshCached标识。这将略降低性能,但将尊重HTTP缓存控制头

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
options:SDWebImageRefreshCached];

添加进程指示器

参看分类:https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage

安装

三个方法来在你的程序中使用SDWebImage

1.使用Cocoapods

2.复制所以的文件到你的程序中

3.作为静态库导入程序

// Your app init code...
return YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片缓存 ios