NSBlockOperation下载图片(三)

原创
2015/03/22 13:34
阅读数 59

NSOperationQueue* queue;
- (void)viewDidLoad
{
   [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
   queue=[[NSOperationQueue alloc]init];
   queue.maxConcurrentOperationCount=10;
}
- (IBAction)downLoadImage:(id)sender {
   NSString* url=@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1814661700,2677631272&fm=23&gp=0.jpg";
   NSBlockOperation* operation=[NSBlockOperation blockOperationWithBlock:^{
       NSData* data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
       UIImage* image=[[UIImage alloc]initWithData:data];
       if (image!=nil)
       {
           [self performSelectorOnMainThread:@selector(getImage:) withObject:image waitUntilDone:YES];
       }
       else
       {
           NSLog(@"下载出错!");
       }
       
   }];
   [queue addOperation:operation];
}
-(void)getImage:(UIImage*)image
{
   self.iv.image=image;
}

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部