美文网首页
SDWebImage框架基本使用

SDWebImage框架基本使用

作者: Z了个L | 来源:发表于2016-08-04 07:17 被阅读27次
  • ViewController

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


// ViewController.m

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloader.h"
#import "UIImage+GIF.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self gif];
}

// 下载图片&设置显示图片&内存缓存&磁盘缓存
// !!!完成后回调是在主线程中处理的
-(void)download1
{
    /*
     第一个参数:要下载图片的URL
     第二个参数:占位图片
     第三个参数:下载选项
     第四个参数:progress 进度回调
        receivedSize:已经下载的数据大小
        expectedSize:图片的中大小
     第五个参数:completed 完成回调(成功|失败)
        cacheType:是否使用了缓存,使用的方式
     */
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://ww1.sinaimg.cn/crop.0.0.720.720.1024/abe7c97cjw8ermn0v2x7nj20k00k0jrz.jpg"] placeholderImage:[UIImage imageNamed:@"Snip20200808_11"] options:SDWebImageLowPriority | SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",(CGFloat)receivedSize/expectedSize);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        NSLog(@"%@--%@",error,[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]);
        switch (cacheType) {
            case SDImageCacheTypeNone:
                NSLog(@"直接下载----");
                break;
            case SDImageCacheTypeDisk:
                NSLog(@"磁盘缓存----");
                break;
            case SDImageCacheTypeMemory:
                NSLog(@"内存缓存----");
                break;

            default:
                break;
        }
    }];


}

// 下载图片&内存缓存&磁盘缓存
// !!!完成后回调是在主线程中处理的
-(void)download2
{
    [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:@"http://img.kumi.cn/photo/6b/42/eb/6b42eb5597c4f174.jpg"] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
          NSLog(@"%f",(CGFloat)receivedSize/expectedSize);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (error == nil) {
            self.imageView.image = image;
        }
    }];
}

// 下载图片&不做任何的缓存处理
// !!!完成后回调是在子线程中处理的
-(void)download3
{
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://img.kumi.cn/photo/6b/42/eb/6b42eb5597c4f174.jpg"] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        // completed是在子线程中处理的
        dispatch_async(dispatch_get_main_queue(), ^{
            //设置图片
            self.imageView.image = image;
        });

    }];
}

- (void)gif
{
    /*
    // 下载动图,gif格式的
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://n.sinaimg.cn/default/20150923/v4dN-fxhzevf0962986.gif"] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        //completed是在子线程中处理的
        dispatch_async(dispatch_get_main_queue(), ^{
            //设置图片
            self.imageView.image = image;
        });

    }];
    */

    /* 该方法不能显示图片的动态过程
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:n.sinaimg.cn/default/20150923/v4dN-fxhzevf0962986.gif"]];
    self.imageView.image = [UIImage imageWithData:data];
     */

    // 可以加载本地动态的gif图片
    self.imageView.image = [UIImage sd_animatedGIFNamed:@"1234"];

}


@end

相关文章

网友评论

      本文标题:SDWebImage框架基本使用

      本文链接:https://www.haomeiwen.com/subject/ekdcsttx.html