[课堂实践与项目]IOS多线程之(2):NSOperation和NSOperationQueue的使用

昨天介绍了NSThread方法的使用,今天介绍 NSOpreation的网络下载。

小的知识点我决定用简洁的方式呈现,以便于查阅。

1.用过选择器进行 NSOpreation的网络加载

1)定义NSInvocationOperation对象。

    
  NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImgae:) object:indexPath];




设置到线程队列中

[self.queue addOperation:operation];

2)downLoadImgage方法

- (void)downLoadImgae:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSString *str = [self.array objectAtIndex:row];
    
    NSURL *url = [NSURL URLWithString:str];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:imageData];
    
    LCMyIndetifiterCell *cell =  (LCMyIndetifiterCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.imageView.image = image;
    
    [cell.activityIndicatorView stopAnimating];
    
    [cell.activityIndicatorView setHidesWhenStopped:YES];
    
    //[self.tableView reloadData];

}


2,通过block语句进行加载

  [self.queue addOperationWithBlock:^(void){
        
        NSInteger row = [indexPath row];
        NSString *str = [self.array objectAtIndex:row];
        
        NSURL *url = [NSURL URLWithString:str];
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:imageData];
        
        
        cell.imageView.image = image;
         
        [cell.activityIndicatorView stopAnimating];
        
        [cell.activityIndicatorView setHidesWhenStopped:YES];

效果如下:



在主线程中更新 ,发现效果不是很理想

- (void)downLoadImgae:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSString *str = [self.array objectAtIndex:row];
    
    NSURL *url = [NSURL URLWithString:str];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    
    UIImage *image = [UIImage imageWithData:imageData];
    
  //  LCMyIndetifiterCell *cell =  (LCMyIndetifiterCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:indexPath,@"indexpath",image,@"image", nil];
    
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:userInfo waitUntilDone:NO];
 //   cell.imageView.image = image;
    
  
    
    //[self.tableView reloadData];

}

- (void)updateUI:(id)sender
{
    NSDictionary *userInfo = sender;
    
    NSIndexPath *indexPath = [userInfo objectForKey:@"indepath"];
    UIImage *image = [userInfo objectForKey:@"image"];
    
    LCMyIndetifiterCell *cell =  (LCMyIndetifiterCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.imageView.image = image;
    
    [cell.activityIndicatorView stopAnimating];
    
    [cell.activityIndicatorView setHidesWhenStopped:YES];

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值