iOS-多线程基础 NSThread /GCD/NSOperation/Runloop


- (void)configurationWithModel:(HomeListModel *)model{

//    self.avatorImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]]];

    self.titleLabel.text = model.jobTitleName;

    self.nameLabel.text= model.docName;

    self.deptLabel.text = model.deptName;

    self.goodatLabel.text = [NSString stringWithFormat:@"擅长: %@", model.docGoodAtNames];


    NSThread*downloadImageThread = [[NSThread alloc]initWithBlock:^{

        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]];

        UIImage*image = [UIImage imageWithData:imgData];

        [self performSelectorOnMainThread:@selector(setImageWithImage:) withObject:image waitUntilDone:YES];

    }];

    downloadImageThread.name=@"downloadImageThread";

    NSLog(@"%d", downloadImageThread.isMainThread);

    [downloadImageThread start];

}

- (void)setImageWithImage:(UIImage *)image {

    [self.avatorImageView setImage:image];

}



    dispatch_queue_global_t downloadImageQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_queue_main_t mainQueue = dispatch_get_main_queue();

    dispatch_async(downloadImageQueue, ^{

        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]];

        UIImage*image = [UIImage imageWithData:imgData];

        dispatch_async(mainQueue, ^{

            self.avatorImageView.image= image;

        });

    });

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]];

        UIImage*image = [UIImage imageWithData:imgData];

        dispatch_async(dispatch_get_main_queue(), ^{

            self.avatorImageView.image= image;

        });

    });



    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSBlockOperation *operation = [[NSBlockOperation alloc] init];

    [operation addExecutionBlock:^{

        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]];

        UIImage*image = [UIImage imageWithData:imgData];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            self.avatorImageView.image= image;

        }];

    }];

//    [queue addOperationWithBlock:^{

//        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]];

//        UIImage *image = [UIImage imageWithData:imgData];

//        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

//            self.avatorImageView.image = image;

//        }];

//    }];

    [queueaddOperation:operation];



你可能感兴趣的:(iOS-多线程基础 NSThread /GCD/NSOperation/Runloop)