美文网首页
多线程05 SDWebImage NSCache 主子线程区别

多线程05 SDWebImage NSCache 主子线程区别

作者: xwf_code | 来源:发表于2016-10-25 21:26 被阅读0次

注册通知处理内存警告

// `object:nil` : 表示任何对象发送内存警告的通知,我都可以接收到
// 提示 : 当你需要注册跟应用程序相关的通知时,可以选择在init方法里面做

[[NSNotificationCenter defaultCenter] 
addObserver:self selector:@selector(cleanMemCache) 
name:UIApplicationDidReceiveMemoryWarningNotification 
object:nil];
//上面@selector(cleanMemCache)就是处理的方法 表示任何对象发送内存警告都会调用这个方法

如果是注册在单例中 由于单例的生命周期和app一样 不需要移除
否则需要重写dealloc移除通知

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

SDWebImage 使用

1.使用IImageView+WebCache category来加载UITableView中cell的图片

[cell.imageView sd_setImageWithURL:
[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

2.使用Blocks,采用这个方案可以在网络图片加载过程中得知图片的下载进度和图片加载成功与否

[cell.imageView sd_setImageWithURL:
[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
completed:^(UIImage image, NSError error, SDImageCacheType cacheType, 
NSURL *imageURL) { ... completion code here ... }];

3.使用SDWebImageManager,SDWebImageManager为UIImageView+WebCache category的实现提供接口。

{ [SDWebImageManager sharedManager]
downloadImageWithURL:imageURL
 options:0
 progress:^(NSInteger receivedSize, NSInteger expectedSize)
 { // progression tracking code } 
completed:^(UIImage image, NSError error, SDImageCacheType cacheType, 
BOOL finished, NSURL imageURL) 
{ if (image)
 { // do something with image } }]};

4.获取SDWebImage的磁盘缓存大小,在项目中有时候会需要统计应用的磁盘缓存内容大小,那么获取图片的缓存大小就是使用这个接口来实现

[SDImageCache sharedImageCache] getSize];

5.清理内存缓存,清理内存中缓存的图片资源,释放内存资源。

[[SDImageCache sharedImageCache] clearMemory];

6.有了清理内存缓存,自然也有清理磁盘缓存的接口

[[SDImageCache sharedImageCache] clearDisk];

7.使用SDWebImage管理者的常用方法 下载图片

SDWebImageManager *manager=[SDWebImageManager sharedManager];
[manager downloadImageWithURL:_url 
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
 //这里是计算图片的下载完成度的
//receivedSize已完成
//expectedSize总完成 用已完成除以总的完成得到下载的完成度
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) 
{
self.testImageView.image=image;
//这里表示图片已经下载完成后的操作(主线程) 所以要在这里给图片赋值 }];

8.渐进式下载,下载多少,展示多少

[self.testImageView sd_setImageWithURL:_url 
placeholderImage:nil 
options:SDWebImageProgressiveDownload 
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        //这里是计算图片的下载完成度的
        //receivedSize已完成
        //expectedSize总完成 用已完成除以总的完成得到下载的完成度
        float progress = (float)receivedSize / expectedSize;
        NSLog(@"%f - %@",progress,[NSThread currentThread]);
    }
 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
 {
        NSLog(@"图片下载完成 %@",[NSThread currentThread]);
        //这里表示已经完成图片赋值操作之后需要执行什么代码 就在这里写(主线程)
}];

9.加载GIF

[self.iconImgView sd_setImageWithURL:self.URL];

NSCache

用法和字典差不多
坑点 : 一旦在didReceiveMemoryWarning这个方法里面调用NSCacheremoveAllObjects方法,有可能会使NSCache失效的
当自己定义方法,调用removeAllObjects就不会有问题

实例化内存缓存池
NSCache *cache = [[NSCache alloc] init];

设置NSCache的缓存成本 : 以缓存的个数为例,最多缓存5个对象
cache.countLimit = 5;

设置代理,测试countLimit是否有效
cache.delegate = self;
当有对象被移除时,就会自动的调用,此处测试countLimit是否有效
- (void)cache:(NSCache *)cache willEvictObject:(id)obj
{
    // obj : 即将被移除的对象
    NSLog(@"remove = %@",obj);
}

问题 : 内存暴涨
解决 : 在循环一开始就手动创建一个自动释放池

long long largeNumber = 10000000;

for (int i = 0; i < largeNumber; ++i) {
    
    // 每循环一次,就释放一次(出了自动释放池的作用域,就释放一次)
    @autoreleasepool {
        NSString *str = @"Hello World";
        str = [str stringByAppendingFormat:@" - %d", i];
        str = [str uppercaseString];
    }
}

主子线程区别

请查找基础笔记-通知中心

相关文章

  • 多线程05 SDWebImage NSCache 主子线程区别

    注册通知处理内存警告 如果是注册在单例中 由于单例的生命周期和app一样 不需要移除否则需要重写dealloc移除...

  • 性能优化

    1.nscache 2.多线程

  • NSCache

    NSCache 是线程安全的,在多线程操作中,不需要对Cache进行加锁,NSCache的key只是对对象的强引用...

  • SDWebImage&&NSRunloop笔记

    SDWebImage: NSCache: NSRunloop: GCD定时器: 线程的任务执行完之后就会进入死亡状...

  • 容易被人忽略的NSCache

    第一次见到NSCache,是在SDWebImage中。SDWebImage的内存缓存机制就是通过NSCache实现...

  • 从多线程角度看SDWebImage

    目录 简介 适合人群 SDWebImage中的多线程设计到的多线程实现技术主要线程/队列数据保护方案 妙处与启发 ...

  • iOS-网络多线程文集目录

    多线程概览pthreadNSThreadGCDNSOperation SDWebImage库 运行循环概览CFRu...

  • iOS多线程.md

    2018-05-22 iOS多线程-概念iOS多线程:『pthread、NSThread』详尽总结 多线程-概念图...

  • day11-04/05-多线程(线程练习)

    day11-05-多线程(线程运行状态)

  • NSCache

    NSCache是什么? NSCache是苹果官方提供的缓存类,在AFNetWorking和SDWebImage等主...

网友评论

      本文标题:多线程05 SDWebImage NSCache 主子线程区别

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