• 多线程知识点(四)


    1. 使用SDWebImage下载图片

    第一步:#import "UIImageView+WebCache.h"

    第二步:[self.iconImageview sd_setImageWithURL:[NSURL URLWithString:model.icon]];

    2. 使用layoutSubviews记得[super layoutSubviews]

    1. 使用Block 的三步:(以子线程下载为例)

    1)             在下载工具里面添加Block的属性

    2)             在工具类的下载方法的下载完成的Block里面调用定义好的block属性

    3)             在需要进行下载操作的类里面,导入工具类,将工具类的Block属性进行赋值(通过Block的set方法),block回调时就会调用该类里面实现定义好的Block里面的内容

    5.NSAssert(self.finishedBlcok != nil, @"self.finishedBlcok 为nil 哈哈");断言

    自定义的下载工具类里面的下载实现

    +(instancetype)DownloadImgWithUrlstr:(NSString *)urlstr finishedBlcok:(void (^)(UIImage *img))finishedBlcok

    {

        DownloadOperation *op = [[DownloadOperation alloc]init];

        op.urlstr = urlstr;

        op.finishedBlcok = finishedBlcok;

        return op;

    }

    - (void)main

    {

        //子线程不能够访问主线程的自动释放池

        //创建自动释放池

        @autoreleasepool {

           

            //断言

            NSAssert(self.finishedBlcok != nil, @"self.finishedBlcok 为nil 哈哈");

            NSLog(@"%@ %@",self.urlstr,[NSThread currentThread]);

           

            // 图片下载

             //模拟网络耗时

            [NSThread sleepForTimeInterval:2.0];

            NSURL *url = [NSURL URLWithString:self.urlstr];

            NSData *data = [NSData dataWithContentsOfURL:url];

            UIImage *img = [UIImage imageWithData:data];

       

            //刷新ui回到主线程

            [[NSOperationQueue mainQueue]addOperationWithBlock:^{

                // 调用

    //            if (self.finishedBlcok) {

    //               

    //            }

                self.finishedBlcok(img);

            }];

           

        }

    }

    6.取消操作,正在进行的操作也要停止下来的操作

    [self.queue cancelAllOperations];

    在下载成功时,刷新UI之前停止下来

    //取消操作

            if (self.isCancelled) {

                return;

            }

           

    7.下载多个操作时,点击下载,取消之操作下载操作,保留最后的操作如果再次点击屏幕获取的图片地址与之前保存的图片地址不一样,那么取消之前的图片下载的操作

    if (![model.icon isEqualToString:self.currentStr]) {

            //取消上次操作

            [self.operationCache[self.currentStr] cancel];

    }

    8.模仿SD创建下载操作的单例

    +(instancetype)sharadDownloadOperationManager

    {

        static DownloadOperationManager *manager;

       

        //一次性执行实现单例

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            if (manager == nil) {

                manager = [[DownloadOperationManager alloc]init];

            }

        });

        return manager;

    }

    9.取消下载操作的三个步骤

    //判断urlstr是否为空

        if (!键值) {

            return;

    }

    //取消操作

    [self.operationCache[urlstr] cancel];

    //从操作缓存池移除已经取消的下载图片的操作

     [self.operationCache removeObjectForKey:urlstr];

    10.为分类添加属性:

    采用运行时机制

    #import <objc/runtime.h>

    重写set  get 方法

    -(NSString *)name

    {

        /*

         参数1:添加属性的对象

         参数2:添加属性对应的key

         */

        return objc_getAssociatedObject(self, "itcast");

    }

    -(void)setName:(NSString *)name

    {

        /*

            参数1:添加属性的对象

            参数2:添加属性对应的key

            参数3:添加属性的名称

            参数4:修饰添加属性的修饰符

         */

        objc_setAssociatedObject(self, "itcast", name, OBJC_ASSOCIATION_COPY_NONATOMIC);

    }

     

    11.老师写的模型类,高大上

    +(instancetype)appInfoWith:(NSDictionary *)dic

    {

        AppModel *model = [[AppModel alloc]init];

        //kvc

        [model setValuesForKeysWithDictionary:dic];

        return model;

    }

    +(NSArray *)appInfos

    {

        //路径

        NSString *path = [[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil];

        //接收数据

        NSArray *arr = [NSArray arrayWithContentsOfFile:path];

       

        //可变数组

        NSMutableArray *marr = [NSMutableArray arrayWithCapacity:10];

       

        //遍历

        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            AppModel *model = [self appInfoWith:obj];

            [marr addObject:model];

        }];

       

        //进行copy操作可以把可变数组变为不可变数组

        return marr.copy;

       

    }

     

  • 相关阅读:
    解决Requires: libc.so.6(GLIBC_2.14)(64bit)错误解决方法
    Linux下安装rpm出现error: Failed dependencies
    使用WordPress搭建个人博客
    Log4j不写入日志文件排错记录
    JAVA EE,JAVA SE,JAVA ME,JDK,JRE,JVM之间的区别
    [转]【maven】解决Missing artifact jdk.tools:jdk.tools:jar:1.6
    Eclipse新建项目介绍
    Android开发-API指南-<permission-group>
    Android开发-API指南-<permission>
    Android开发-API指南-<path-permission>
  • 原文地址:https://www.cnblogs.com/chaoyueME/p/5574927.html
Copyright © 2020-2023  润新知