​iOS之路13-SDWebImage 框架的基本使用

SDWebImage 框架的基本使用

  1. 首先要看readme

    这是我的翻译

    这个库提供了一个UIImageView的分类,可以从网络远端加载图片

    它提供了:

  1. 一个UIImageView的分类加载网络图片和缓存管理

  2. 异步加载图片

  3. 异步内存和磁盘缓存是自动缓存处理

  4. 支持GIF

  5. 支持网页格式

  6. 支持背景图片解压缩

  7. 在一个期限内,相同的URL将不会被重复下载

  8. 错误的URL将不会被重试下载

  9. 在这个时间内,主线程将不会被阻碍

  10. 使用的是GCDARC

  11. 支持Arm64处理器

 

1.通过git 命令下载SDWebImage,这样可以得到最新版本

git clone https://github.com/rs/SDWebImage.git

 

2.不知道官方文档为什么使用尖括号导入,注意改成双引号

#import <SDWebImage/UIImageView+WebCache.h>

Using UIImageView+WebCache category with UITableView

使用 UIImageView+WebCache 分类用于TableView

Just #import the UIImageView+WebCache.h header, and call the sd_setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.

仅仅需要导入 UIImageView+WebCache.h 头文件,然后调用sd_setImageWithURL:方法在tabelView的cellForRowAtIndexPath方法中使用,这是从网络异步加载图片到缓存的方法都实现了

#import "ViewController.h"
// 导入头文件的时候注意使用双引号
#import "SDWebImage/UIImageView+WebCache.h"

@interface ViewController ()<UITableViewDataSource>
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
 
- (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];
    }
    
    // 这里的图片url要自己更换一下,占位图片也自己选择一下
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    cell.textLabel.text = @"My Text";
    return cell;
}

直接运行即可,测试运行效果

​iOS之路13-SDWebImage 框架的基本使用_第1张图片

With blocks, you can be notified about the image download progress and whenever the image retrieval has completed with success or not:

neither your success nor failure block will be call if your image request is canceled before completion.

这是带block的回调的方法,你可以关注图片下载进度和图片是否下载成功

除非你的图片请求被取消在调用block之前,否则无论block是否成功还是失败都将会被调用

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 if(error) {
                                     NSLog(@"下载失败");
                                 }else {
                                     NSLog(@"下载成功");
                                 }
                             }];
                             
运行结果:
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功
SDWebImage使用[6118:399525] 下载成功

Using SDWebImageManager

使用单例管理类

The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the asynchronous downloader with the image cache store. You can use this class directly to benefit from web image downloading with caching in another context than a UIView (ie: with Cocoa).

Here is a simple example of how to use SDWebImageManager:

这个单例管理类是异步下载到图片缓存都做了,

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSURL *imageURL = [NSURL URLWithString:@"http://img1.3lian.com/img2008/05/003/023.jpg"];
    [manager downloadImageWithURL:imageURL
                          options:0
                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                             NSLog(@"加载中 ");
                         }
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                            if (image) {
                                NSLog(@"图片缓存完成");
                            }
                        }];
    // 打印沙盒目录,查看Cache目录下会有相应的图片
    NSString *path = NSHomeDirectory();//主目录
    NSLog(@"NSHomeDirectory:%@",path);


你可能感兴趣的:(框架的基本使用)