iOS常识源码

SDWebImage

2020-10-10  本文已影响0人  CyberDunk1997

SDWebImage主要结构

1. 基本使用

下载策略

下载策略

1.1 SDWebManager类

    [[SDWebImageManager sharedManager] loadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        <#code#>
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        <#code#>
    }];

1.2 SDWebManagerDownload类

    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        <#code#>
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {

        [NSOperationQueue mainQueue]addOperationWithBlock:^{
           //在这里更新UI
        }

    }];

1.3 UIImageView分类

    /*
     第1个参数:下载图片的url
     第2个参数:占位图片
     第3个参数:下载策略
     第4个参数:progress 进度回调
                receivedSize:已经下载图片的数据大小
                expectedSize:图片总共的数据大小
     第4个参数:completed 完成回调
                image:要下载的图片
                error:错误信息
                cacheType:缓存类型(0代表从互联网下载,1代表从磁盘获得,2代表从内存获得)
                imageURL:图片url
     */
   [_image sd_setHighlightedImageWithURL:url 
                                 options:[UIImage imageNamed:@"1"] 
                                progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) 
    {
                                NSLog(@"%ld,%ld,%@",(long)receivedSize,(long)expectedSize,targetURL);
    } 
                               completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) 
    {
                                NSLog(@"%@,%@,%ld",image,error,(long)cacheType);
                                [self.image setImage:image];
    }];

内存警告

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    //1.清空缓存
    //clearDisk:直接删除文件夹,然后重新创建
    //cleadDisk:清除过期缓存(7天),计算当前缓存大小,和设置的最大缓存数量比较,若果超出那么会继续删除(按照文件创建的先后顺序删除)
    [[SDWebImageManager sharedManager].imageCache clearWithCacheType:0 completion:^{
        <#code#>
    }];
    //2.取消当前所有操作
    [[SDWebImageManager sharedManager] cancelAll];
    
}

NSCache

1.NSCache是专门用来进行缓存处理的

2.NSCache的属性

3.NSCache的方法

[func setObject(ObjectType, forKey: KeyType)]
Sets the value of the specified key in the cache.

[func setObject(ObjectType, forKey: KeyType, cost:Int )]
Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.

[func removeObject(forKey: KeyType)]
Removes the value of the specified key in the cache.

[func removeAllObjects()]
Empties the cache.

其他

mac进行MD5加密的方法:命令行echo -n "http://...../..../... .jpeg" |md5

对系统的通知方法进行监听,当接收到内存警告会清理内存,当接收到用户点击Home键会清理磁盘,当接收到即将进入后台会清理后台内存

可变字典 ——> NSCache

获得传入图片的第一个字节,在判断图片类型时,匹配第一个字节,0xFF是jpeg,0x89是png,0x47是gif...

通过网络请求NSURLSession请求数据,如果数据较大,会多次接收数据,拼接成一个完整数据

超时时间15.0s,

上一篇 下一篇

猜你喜欢

热点阅读