美文网首页
iOS-NSOperation-自定义

iOS-NSOperation-自定义

作者: 星之夜下 | 来源:发表于2020-01-18 10:37 被阅读0次

 #pragma mark-设置代理和代理方法

MFdownLoadOperation.h文件

 @class MFdownLoadOperation;

 @protocol MFdownLoadOperationDelegate

-(void)downLoadOperation:(MFdownLoadOperation*)operation didFishedDownLoad:(UIImage *)image;

 @end

 @interface MFdownLoadOperation : NSOperation

 @property(nonatomic,copy)NSString *url;

 @property(nonatomic,strong)NSIndexPath *indexPath;

@property(nonatomic,strong)iddelegate;

 @end

MFdownLoadOperation.m文件

 #import "MFdownLoadOperation.h"

 @implementation MFdownLoadOperation

 -(void)main {

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

NSData *data=[NSData dataWithContentsOfURL:url];

UIImage *imgae=[UIImage imageWithData:data];

 NSLog(@"--%@--",[NSThread currentThread]);

//图片下载完毕后,通知代理

   if ([self.delegate respondsToSelector:@selector(downLoadOperation:didFishedDownLoad:)]) {

 dispatch_async(dispatch_get_main_queue(), ^{//回到主线程,传递数据给代理对象

   [self.delegate downLoadOperation:self didFishedDownLoad:imgae];

 });

   }

 }

 @end

实现使用

@interface MFViewController ()<MFdownLoadOperationDelegate>

@property(nonatomic,strong)NSArray *apps;

@property(nonatomic,strong)NSOperationQueue *queue;

@property(nonatomic,strong)NSMutableDictionary *operations;

@property(nonatomic,strong)NSMutableDictionary *images;

 @end

@implementation YYViewController

#pragma mark- 懒加载apps

 -(NSArray *)apps {

   if (_apps==nil) {

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

   NSArray *tempArray=[NSArray arrayWithContentsOfFile:path];      

         //字典转模型

      NSMutableArray *array=[NSMutableArray array];

        for (NSDictionary *dict in tempArray) {

            YYappModel *app=[YYappModel appModelWithDict:dict];

             [array addObject:app];

         }

        _apps=array;

     }

     return _apps;

 }

 #pragma mark-懒加载queue

 -(NSOperationQueue *)queue

 {

     if (_queue==Nil) {

         _queue=[[NSOperationQueue alloc]init];

         //设置最大并发数为3

        _queue.maxConcurrentOperationCount=3;

     }

     return _queue;

 }

 #pragma mark-懒加载operations

 -(NSMutableDictionary *)operations

 {

     if (_operations==Nil) {

        _operations=[NSMutableDictionary dictionary];

     }

     return _operations;

 }

 #pragma mark-懒加载images

 -(NSMutableDictionary *)images {

     if (_images==Nil) {

         _images=[NSMutableDictionary dictionary];

     }

    return _images;

 }

 #pragma mark-数据源方法

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 {

     return self.apps.count;

 }

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *ID=@"ID";

 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];

 if (cell==nil) {

     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    }

     YYappModel *app=self.apps[indexPath.row];

   cell.textLabel.text=app.name;

   cell.detailTextLabel.text=app.download;

    //保证一个url对应一个image对象

     UIImage *image=self.images[app.icon];

     if (image) {//缓存中有图片

         cell.imageView.image=image;

     }else      //  缓存中没有图片,得下载

     {

       //先设置一张占位图片

        cell.imageView.image=[UIImage imageNamed:@"57437179_42489b0"];

      MFdownLoadOperation *operation=self.operations[app.icon];

        if (operation) {//正在下载

         }else  //当前没有下载,那就创建操作

         {

           operation=[[MFdownLoadOperation alloc]init];

           operation.url=app.icon;

            operation.indexPath=indexPath;

          operation.delegate=self;

        [self.queue addOperation:operation];//异步下载

         self.operations[app.icon]=operation;

       }

     }

   return cell; }

 -(void)downLoadOperation:(YYdownLoadOperation *)operation didFishedDownLoad:(UIImage *)image {

   //1.移除执行完毕的操作

    [self.operations removeObjectForKey:operation.url];

     //2.将图片放到缓存中

    self.images[operation.url]=image;

   [self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

 }

@end

相关文章

  • iOS-NSOperation-自定义

    #pragma mark-设置代理和代理方法 MFdownLoadOperation.h文件 @class MFd...

  • Dialog

    安卓dialog的使用+如何自定义dialog自定义Dialog自定义Dialog 自定义

  • django的自定义filter和自定义simple_tag

    django的自定义filter和自定义simple_tag 自定义filter: 自定义filter: 简单示例...

  • 自定义tabbarController

    要自定义tabBarController,就要自定义tabBar.要自定义tabBar就要自定义item.所以,咱...

  • 第三方

    ZYSideSlipFilter 侧边栏条件筛选器,支持自定义事件,自定义筛选栏目,自定义所有。。。样式完全自定义...

  • Android 高德地图 自定义Location小蓝点

    设置自定义定位蓝点 自定义Location小蓝点,自定义功能

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • Android相关知识点博客记录

    自定义属性 Android自定义View(二、深入解析自定义属性) Android中XML的命名空间、自定义属性 ...

  • CocoaLumberjack源码分析

    1.使用 自定义custom context,自定义flag 自定义日志的格式 自定义日志级别,取消DDLog实现...

  • Android View(转)

    自定义View的原理自定义View基础 - 最易懂的自定义View原理系列自定义View Measure过程 - ...

网友评论

      本文标题:iOS-NSOperation-自定义

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