[Classroom practices and projects] IOS network ------- NSInvocationOperation and NSOper

Source: Internet
Author: User

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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.