SDWebImage的简单使用

SDWebImage是一款优秀的图片加载和缓存类库,比如在tableview上直接采用UIImageView对大量网络图片进行加载,在运行程序时会出现卡顿, 而SDWebImage通过对UIImageView添加了一个扩展类,来实现对网络图片的优化加载,缓存和管理。

使用方法:

1.导入头文件:

#import <SDWebImage/UIImageView+WebCache.h>

2.在需要加载图片的地方:

通过此方法,SDWebImage就为我们实现了网络图片的加载,缓存。它的缓存图片以url为key进行保存,url与图片是一一对应关系。所以请求同一个url时,SDWebImage会从缓存中取得图片。一般的情况下用此方法可以满足我们的应用要求。

- (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];
    }

    // 此处使用SDWebImage的方法来加载图片,
// URLWithString处的参数为:我们需要加载图片的url。
//placeholderImage处的参数为:网络图片尚未加载成功时显示的图片。
   [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; }

3.上诉方法在面对请求同一个url,而这张图片在服务器端更新了,那么就会出现问题。解决的办法是:(附:options所有方法)

  // 此方法多了一个options参数,通过选择这个参数的类型来实现不同的需求,解决上面的问题,只需要使用SDWebImageRefreshCached ,刷新缓存的方式就   可以实现
  -(void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
     //失败后重试
     SDWebImageRetryFailed = 1 << 0,      
     //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
     SDWebImageLowPriority = 1 << 1,     
     //只进行内存缓存
     SDWebImageCacheMemoryOnly = 1 << 2,      
     //这个标志可以渐进式下载,显示的图像是逐步在下载
     SDWebImageProgressiveDownload = 1 << 3,      
     //刷新缓存
     SDWebImageRefreshCached = 1 << 4,      
     //后台下载
     SDWebImageContinueInBackground = 1 << 5,      
     //NSMutableURLRequest.HTTPShouldHandleCookies = YES;      
     SDWebImageHandleCookies = 1 << 6,      
     //允许使用无效的SSL证书
     //SDWebImageAllowInvalidSSLCertificates = 1 << 7,   
     //优先下载
     SDWebImageHighPriority = 1 << 8,      
     //延迟占位符
     SDWebImageDelayPlaceholder = 1 << 9,
     //改变动画形象
     SDWebImageTransformAnimatedImage = 1 << 10,

4.对于SDWebImageManager的使用:

可以直接通过SDWebImageManager来下载图片,并缓存下来

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imageURL
                      options:0
                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                         // 操作进度
                     }
                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                         if (image) {
                             // 如果缓存成功,通过缓存的图片进行一些操作
                         }
                     }];

5.独立异步图片下载器:

   实现单独的图片下载功能

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
                            }
                        }];
原文地址:https://www.cnblogs.com/moxuexiaotong/p/4933876.html