多线程 NSOperation

//

//  ViewController.m

//  NSOperation

//

//  Created by dc008 on 15/12/24.

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#define ROW 5

#define COLUMN 3

#define IMAGE_COUNT ROW*COLUMN

#define WIDTH 100 //图片宽

#define HEIGHT WIDTH //图片高

#import "ViewController.h"


@interface ViewController ()

{

    NSMutableArray *_imageViews;

    NSArray *array;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self layout];

    array = @[@"http://pic12.nipic.com/20110121/3556745_181933155140_2.jpg",

              @"http://pic31.nipic.com/20130722/12196046_160624534194_2.jpg",

              @"http://pic4.nipic.com/20091118/3047572_163336087407_2.jpg",

              @"http://pic.nipic.com/2008-05-29/200852973848511_2.jpg",

              @"http://pic41.nipic.com/20140504/18401726_095421197182_2.jpg",

              @"http://pic51.nipic.com/file/20141024/13035204_155709166956_2.jpg",

              @"http://pic36.nipic.com/20131125/8821914_113934565000_2.jpg",

              @"http://pic31.nipic.com/20130722/12196046_160637651196_2.jpg",

              @"http://pic28.nipic.com/20130412/3718408_231850588113_2.jpg",

              @"http://pica.nipic.com/2007-06-06/20076615744388_2.jpg",

              @"http://pic23.nipic.com/20120901/8737320_222046006000_2.jpg",

              @"http://pic12.nipic.com/20110119/3556745_180736335138_2.jpg",

              @"http://pic27.nipic.com/20130226/6806713_165327321179_2.jpg",

              @"http://pic18.nipic.com/20111202/8920105_164704212146_2.jpg",

              @"http://img.taopic.com/uploads/allimg/110914/8879-11091423505866.jpg"

              ];

}



- (void)layout{

    _imageViews = [NSMutableArray array];

    for (int r = 0; r < COLUMN; r++) {

        for (int c = 0; c < ROW; c++) {

            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(18.75 + r *118.75, 20+ c *118.75,WIDTH , HEIGHT)];

            imageView.backgroundColor = [UIColor orangeColor];

            [self.view addSubview:imageView];

            [_imageViews addObject:imageView];

        }

        

    }

    //添加按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(50, 600, 275, 30);

    [button setTitle:@"加载图片" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(useMultiThread) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}


- (void)loadImage : (NSNumber *)index {

    

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://pic31.nipic.com/20130722/12196046_160624534194_2.jpg"]]];

    

    //更新UI界面,调用主线程队列

    [[NSOperationQueue mainQueue]addOperationWithBlock:^{

        [self updateImage:@[data,index]];

    }];

}


- (void)updateImage : (NSArray *)arr{

    UIImage *image = [UIImage imageWithData:arr[0]];

    UIImageView *imageView = _imageViews[[arr[1]intValue] ];

    imageView.image = image;

}


- (void)useMultiThread{

  //1.创建一个操作队列

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

    operationQueue.maxConcurrentOperationCount = 5;//设置最大并发线程数

  //2.向队列添加操作

//    for (int i= 0 ; i<IMAGE_COUNT; i++) {

        //方法1.创建操作块,添加到队列

//        NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{

//            [self loadImage : [NSNumber numberWithInt:i]];

//            

//        }];

//        [operationQueue addOperation:blockOperation];

//

        //方法2.祷文invocation

//        NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];

//        [operationQueue addOperation:invocationOperation];

        

        //方法3.控制线程的执行顺序

        NSBlockOperation *lastOperation = [NSBlockOperation blockOperationWithBlock:^{

            [self loadImage:@(IMAGE_COUNT-1)];

        }];

        for (int j = 0; j<IMAGE_COUNT-1; j++) {

            NSBlockOperation *normalOp = [NSBlockOperation blockOperationWithBlock:^{

                [self loadImage:[NSNumber numberWithInt:j]];

            }];

            //设置依赖操作(普通依赖最后)

            [normalOp addDependency:lastOperation];

            [operationQueue addOperation:normalOp];

        }

    [operationQueue addOperation:lastOperation];

    

    //}

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


转载于:https://my.oschina.net/u/2499773/blog/550622

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值