SDWebImage框架研究

                                                                    SWDebImage框架研究

        该库提供的UIImageView一个类别与远程图像从网站未来的支持。它提供:一个类的UIImageView添加Web图像和缓存管理的可可触摸框架,异步下载图像具有自动缓存过期处理异步内存+磁盘映像缓存,动画GIF支持的WebP格式支持背景图像解压,同样的网址不会被几次.

一个保证,假的网址将不被一次又一次地重试

一个保证,主线程将永远不会被阻止演出!

使用GCD和ARCArm64支持.

注:SDWebImage的3.0版是不完全向后兼容2.0和需要iOS5.1.1最低deployement版本。如果你需要的iOS<5.0的支持,请使用最后2.0版本。

                      Using UIImageView+WebCache category with UITableView


           UIImageView的头WebCache.h,并调用sd_setImageWithURL:placeholderImage:方法从的tableView:cellForRowAtIndexPath的:UITableViewDataSource方法。一切都会为你处理,从异步下载到缓存管理。

#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;

}

Using blocks

使用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 ...}];

注意:如果请求被取消,那么block不会被调用(无论成功还是失败)。

Using Asynchronous Image Caching Independently

也可以独立地使用基于异步的图像缓存存储。SDImageCache维护一个内存缓存和一个可选的磁盘缓存。磁盘高

速缓存的写操作是异步进行的,所以它不会给UI增加不必要的延迟

为了方便,SDImageCache类提供了一个单例,但是如果你想创建单独的缓存命名空间你也可以创建新的实例。

你可以使用imageForKey:方法来查找缓存,如果返回为nil,说明当前图像不拥有缓存。因此你负责生成并缓存它。缓存键(cache key)是一个程序中图像缓存的唯一标识符,他通常是图像的url

SDImageCache *imageCache = [SDImageCache.alloc initWithNamespace:@"myNamespace"];

[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)

{

    // image is not nil if image was found

}];

Handle image refresh(控制图像刷新)

        默认情况下,SDWebImage确实非常积极的缓存。它忽略了所有类型的通过HTTP服务器返回的缓存控制头,并

且没有时间限制地缓存返回的图像。这意味着你的图像url是永远不会改变的、指向图像的静态url。如果指向的图片

发生了变化,那么url也会相应的跟着变化。

如果你不控制你的图像服务器,当它的内容更新时你不能改变它的urlFacebook头像就是这种情况的例子。在这种情况下,你可以使用SDWebImageRefreshCached的标志。这将稍微降低性能,但将会考虑到HTTP缓存控制头:

[imageView setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]

          placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]

                   options:SDWebImageRefreshCached];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值