SDWebImage的介绍与基本使用

什么是SDWebImage

iOS中著名的牛逼的网络图片处理框架

包含的功能:图片下载、图片缓存、下载进度监听、gif处理等等

用法极其简单,功能十分强大,大大提高了网络图片的处理效率

国内超过90%iOS项目都有它的影子

项目地址

https://github.com/rs/SDWebImage

- (void)sd_setImageWithURL:(NSURL *)url;

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;

注意:options是表示枚举值——>主要由以下几个:(在SDWebImageManager.h中)只需要掌握前两个就行了。

SDWebImageRetryFailed = 1 << 0, :默认情况下,如果一张图片下载失败了,就会把这张图片加入黑名单,不会再进行下载。如果传入此枚举变量,会把下载失败的图片从黑名单移除,意思就是如果此图片下载失败了,会重新进行下载。

SDWebImageLowPriority = 1 << 1,:低优先级,UI交互时暂停下载图片。滚动即用户在拖拽的时候,会延迟下载。这样会省资源给用户运行流畅的感觉。而默认情况下是不暂停或延迟的。

SDWebImageCacheMemoryOnly = 1 << 2,:默认情况下是内存和沙盒结合的缓存。此枚举表示仅仅内存缓存,不会把缓存保存到沙盒。

SDWebImageProgressiveDownload = 1 << 3,:此标记允许渐进式下载,就像浏览器那样,下载过程中,图像会逐渐显示出来。

新建工程。导入SDWebImage框架,并导入头文件#import "UIImageView+WebCache.h", 拉进准备好的测试的plist文件,

CZApp.h

<span style="font-size:18px;">//
//  CZApp.h
//  NSoperation之网络图片下载
//
//  Created by apple on 15/10/23.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CZApp : NSObject
@property(nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, strong) NSString *download;

/**
 保存网络下载的图像
 */
@property(nonatomic, strong) UIImage *image;

+(instancetype) appWithDict:(NSDictionary *) dict;
@end</span>
CZApp.m

<span style="font-size:18px;">//
//  CZApp.m
//  NSoperation之网络图片下载
//
//  Created by apple on 15/10/23.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "CZApp.h"

@implementation CZApp
+(instancetype) appWithDict:(NSDictionary *) dict
{
    CZApp *app = [[self alloc] init];
    [app setValuesForKeysWithDictionary:dict];
    return app;
}
@end</span>
viewController.m

<span style="font-size:18px;">//
//  ViewController.m
//  NSoperation之网络图片下载
//
//  Created by apple on 15/10/23.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"
#import "CZApp.h"
#import "UIImageView+WebCache.h"

@interface ViewController ()
// plist文件数据的容器
@property (nonatomic, strong) NSArray *appList;

@end

@implementation ViewController

// 懒加载
-(NSArray *)appList
{
    if (_appList == nil) {
        NSArray *dicArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
        // 字典转模型
        NSMutableArray *arryM = [NSMutableArray array];
        for(NSDictionary *dict in dicArray){
            CZApp *app = [CZApp appWithDict:dict];
            [arryM addObject:app];
        }
        _appList = arryM;
    }
    return _appList;
}

-(void)viewDidLoad
{
    NSLog(@"%@", NSHomeDirectory());
}

#pragma mark - 实现数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.appList.count;
}

// cell里面的imageView子控件是懒加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"AppCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 给Cell设置数据
    CZApp *app = self.appList[indexPath.row];
    cell.textLabel.text = app.name;
    cell.detailTextLabel.text = app.download;
    
    // 显示图片
    //    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"user_default"]];
    
    // 第二种用法
    // receivedSize : 图片已经下载了多少   expectedSize 图片的总大小
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon]  placeholderImage:[UIImage imageNamed:@"user_default"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        float progress =  receivedSize*1.0/expectedSize;
        NSLog(@"下载的进度= %f", progress);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"%@", [NSThread currentThread]);
    }];
    return cell;
}
@end
/**  只需要掌握前两个就行了。
 注意:options是表示枚举值——> 主要由以下几个:(在SDWebImageManager.h中)
 SDWebImageRetryFailed = 1 << 0, :默认情况下,如果一张图片下载失败了,就会把这张图片加入黑名单,不会再进行下载。如果传入此枚举变量,会把下载失败的图片从黑名单移除,意思就是如果此图片下载失败了,会重新进行下载。
 
 SDWebImageLowPriority = 1 << 1,:低优先级,UI交互时暂停下载图片。滚动即用户在拖拽的时候,会延迟下载。这样会省资源给用户运行流畅的感觉。而默认情况下是不暂停或延迟的。
 
 SDWebImageCacheMemoryOnly = 1 << 2,:默认情况下是内存和沙盒结合的缓存。此枚举表示仅仅内存缓存,不会把缓存保存到沙盒。
 
 SDWebImageProgressiveDownload = 1 << 3,:此标记允许渐进式下载,就像浏览器那样,下载过程中,图像会逐渐显示出来。
 */
</span>
运行结果正常,后台第一次运行如下:

打开沙盒路径,找到缓存如下:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值