Yesterday we introduced the use of the NSThread method, and today we will introduce the network download of NSOpreation.
I decided to present small knowledge points in a concise manner for reference.
1. Use a selector to load NSOpreation Networks
1) define the NSInvocationOperation object.
NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImgae:) object:indexPath];
Set to thread queue
[self.queue addOperation:operation];
2) downLoadImgage Method
- (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. load data using block statements
[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];
The effect is as follows: