@SDWebImage提供一个UIImageView的类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征.
@SDWebImage的导入
1.https://github.com/rs/SDWebImage 下载SDWebImage开源包
2.将类包拖入工程,再导入MapKit.framework、ImageIO.framework两个框架
3.SDWebImage是支持ARC的,在MRC的工程中要注意,可参考MRC工程配置ARC
4.注意:SDWebImage 3.0不向后兼容2.0并且最低需要iOS 5.0 的版本,而且提供的方法多数是Blcok形式
@目前来说,主要用到了"UIImageView+WebCache.h",给出一个代码示例:
它是UIImageView的一个类目,在要使用它的类中加入#import "UIImageView+WebCache.h",调用 setImageWithURL:placeholderImage:方法。从异步下载到缓存管理,一切都会为你处理。
查看图片是否进了缓存:
@得到1个图片的url用SDWebImage缓存后的文件名
@其他
SDWebImage是个比较大的类库,还有其他一些类的用法,自己不太了解,欢迎大家留言给出意见,一起学习,下面给出一些比较好的博客文章(转的)
http://blog.csdn.net/shenjx1225/article/details/10444449
Using blocks
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (image)
{
// do something with image
}
}];
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSUInteger receivedSize, long long expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
// do something with image
}
}];
SDImageCache *imageCache = [SDImageCache.alloc initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
下面的示例在应用程序的委托中设置一个过滤器,在使用它的缓存键之前将从URL中删除任何查询字符串(The following example sets a filter in the application delegate that will remove any query-string from the URL before to use it as a cache key):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SDWebImageManager.sharedManager.cacheKeyFilter:^(NSURL *url)
{
url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
return [url absoluteString];
};
// Your app init code...
return YES;
}
http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/
如果你不控制你的图像服务器,当它的内容更新时你不能改变它的url。Facebook头像就是这种情况的例子。在这种情况下,你可以使用SDWebImageRefreshCached的标志。这将稍微降低性能,但将会考虑到HTTP缓存控制头:
[imageView setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
options:SDWebImageRefreshCached];
@好像是SDWebImage2.0的流程介绍 http://blog.csdn.net/uxyheaven/article/details/7909373
iOS网络编程(三) 异步加载及缓存图片---->SDWebImage,布布扣,bubuko.com
iOS网络编程(三) 异步加载及缓存图片---->SDWebImage
原文:http://www.cnblogs.com/yjg2014/p/3835170.html