iOS多线程 一一 SDWebImage框架的基本使用

相关博客: http://www.jianshu.com/p/be9a0a088feb


//
//  ViewController.m
//  05掌握-SDWebImage框架的基本使用
//
//  Created by 朝阳 on 2017/11/28.
//  Copyright © 2017年 sunny. All rights reserved.
//

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

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

@end

@implementation ViewController

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

//下载图片且需要获取下载进度
// 作 内存缓存&磁盘缓存
- (void)download
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] placeholderImage:[UIImage imageNamed:@"qq"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        switch (cacheType) {
            case SDImageCacheTypeNone:
                NSLog(@"下载图片");
                break;
            case SDImageCacheTypeDisk:
                NSLog(@"磁盘缓存");
                break;
            case SDImageCacheTypeMemory:
                NSLog(@"内存缓存");
                break;
                
            default:
                break;
        }
    }];
    NSLog(@"%@",NSHomeDirectory());
    
}

// 只需要简单获取一张图片,不设置
// 作 内存缓存&磁盘缓存
- (void)download1
{
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        NSLog(@"%@--UI",[NSThread currentThread]);
        // image 就是通过url获得的图片
        self.imageView.image = image;
        
    }];
}

// 不需要任务的缓存处理
// 没有做任务的缓存处理
- (void)download2
{
    //data: 图片的二进制数据
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/459653.jpg"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",0.1 * receivedSize / expectedSize);
        
    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        
//        NSLog(@"%@--UI",[NSThread currentThread]); // 子线程中刷新UI的
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
        
    }];
}

// 播放GIF图片
- (void)playGIF
{
    
    NSURL *url = [NSURL URLWithString:@"http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=3e4079778ad4b31cf0699cbfb2e60b49/c9fcc3cec3fdfc03e6e8818cd53f8794a4c22675.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage sd_animatedGIFWithData:data];
    self.imageView.image = image;

}

// 判断图片类型
- (void)judgeImageType
{
    NSData *imageData = [NSData dataWithContentsOfFile:@"/Users/sunny/Desktop/photo/head.JPG"];
    NSString *typeStr = [NSData sd_contentTypeForImageData:imageData];
    NSLog(@"%@",typeStr);
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white camel

感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值