这个库提供了一个类别,用于支持从网络远程加载的图片的 UIImageView
它提供
UIImageView
类别注意:SDWebImage 的 3.8 版本需要 iOS 7 或更高版本(因为 NSURLSession)。3.7 到 3.0 版本需要 iOS 5.1.1。如果您需要支持 iOS < 5.0,请使用最后的 2.0 版本。
找出 谁在使用 SDWebImage 并将您的应用程序添加到列表中。
API 文档可在 CocoaDocs - SDWebImage 查找。
只需#导入shivImage+WebCache.h头文件,并在UITableView:cellForRowAtIndexPath: UITableViewDataSource方法中调用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,您可以在图像下载进度和图像检索成功或失败时被通知
// 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"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
... completion code here ...
}];
注意:如果您的图像请求在完成之前被取消,则不会调用您的成功或失败块。
SDWebImageManager 是 UIImageView+WebCache 类别背后的类。它将异步下载程序与图像缓存存储器绑定。您可以直接使用此类从除 UIView 之外的其他上下文(例如 Cocoa)中受益于带有缓存的网络图像下载。
以下是如何使用 SDWebImageManager 的一个简单示例
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL: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 *downloader = [SDWebImageDownloader sharedDownloader];
[downloader 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:
。
有时你可能不想使用图像URL作为缓存键,因为URL的一部分是动态的(例如:用于访问控制)。SDWebImageManager提供了一种设置缓存键过滤器的方式,该过滤器接收NSURL作为输入,并输出一个缓存键NSString。
以下示例将设置在应用程序代理中的过滤器,该过滤器将在将其用作缓存键之前从URL中移除任何查询字符串。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
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;
}
UITableView通过单元格中的第一个图像集来确定图像的大小。如果你远程图像的大小与占位符图像不同,你可能会遇到奇怪的形变缩放问题。以下文章提供了一种解决这一问题的方法
http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/
SDWebImage默认进行了非常积极的缓存。它忽略了由HTTP服务器返回的所有类型的缓存控制头,并无限期地缓存返回的图像。这意味着你的图像URL是静态URL,指向永远不会改变的图像。如果指向的图像意外更改,URL的某些部分也应相应更改。
如果你不能控制使用图像服务器,你可能无法在内容更新时更改URL。例如,这就是Facebook头像URL的情况。在这种情况下,你可能会使用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有三种方法
platform :ios, '7.0'
pod 'SDWebImage', '~>3.8'
如果你使用Swift,请确保添加use_frameworks!
并将其目标设置为iOS 8+
platform :ios, '8.0'
use_frameworks!
目前有3个子规格可用:Core
、MapKit
和WebP
(这意味着你可以只安装SDWebImage的一些模块。默认情况下,你只会得到Core
,因此如果你需要WebP
,你需要指定它)。
Podfile示例
pod 'SDWebImage/WebP'
github "rs/SDWebImage"
Swift
如果你使用CocoaPods进行安装
import SDWebImage
如果你手动安装
import WebImage
Objective-C
@import WebImage;
为了访问仓库中的所有文件,你应该克隆它。
git clone --recursive https://github.com/rs/SDWebImage.git
打开“构建设置”选项卡,在“链接”部分找到“其他链接器标志”设置并添加"-ObjC"标志
如果这导致与扩展可选库的框架(例如Parse、RestKit或opencv2)的编译问题,而不是-ObjC标志,请改用
-force_load SDWebImage.framework/Versions/Current/SDWebImage
如果你使用Cocoa Pods并使用任何扩展可选库的框架,如Parsen RestKit或opencv2,而不是-ObjC标志,请使用
-force_load $(TARGET_BUILD_DIR)/libPods.a
还有这个
$(inherited)
在需要使用库的源文件中导入头文件
#import <SDWebImage/UIImageView+WebCache.h>
此时,你的工作空间应该可以无错误地构建。如果你遇到问题,请将其发布到Issue区域,社区可以帮助你解决问题。
所有源代码均根据MIT许可协议许可。