移动开发征服iOS

多线程的简单使用

2017-03-16  本文已影响0人  路灯下的黑猫H

//

//ViewController.m

//Created by S u p e r m a n on 2017/3/14.

//Copyright © 2017年张浩. All rights reserved.

//

#import"ViewController.h"

staticNSIntegerconstAddTag =100;

@interfaceViewController()

@property(nonatomic,strong)UIImageView* imageView;

@property(nonatomic,strong)NSArray* urlArr;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.urlArr=@[@"http://weixue.steptowin.com:8000/data/img/20160411/veuyytthy4b2_320_200.jpg",

@"http://weixue.steptowin.com:8000/data/img/20160528/qcxt7ow9syjo.png",

@"http://weixue.steptowin.com:8000/data/img/20160527/zeol7j6gsto9.png",

@"http://weixue.steptowin.com:8000/data/img/20160413/wag5q4elonh4_320_200.jpg",

@"http://weixue.steptowin.com:8000/data/img/20160411/veuyytthy4b2_320_200.jpg",

@"http://weixue.steptowin.com:8000/data/img/20160528/qcxt7ow9syjo.png",

@"http://weixue.steptowin.com:8000/data/img/20160527/zeol7j6gsto9.png",

@"http://weixue.steptowin.com:8000/data/img/20160413/wag5q4elonh4_320_200.jpg",

@"http://weixue.steptowin.com:8000/data/img/20160411/veuyytthy4b2_320_200.jpg",

@"http://weixue.steptowin.com:8000/data/img/20160528/qcxt7ow9syjo.png",

@"http://weixue.steptowin.com:8000/data/img/20160527/zeol7j6gsto9.png",

@"http://weixue.steptowin.com:8000/data/img/20160413/wag5q4elonh4_320_200.jpg"];

/*简单线程**/

//[self thread];

/*队列操作**/

//[self operation];

/*GCD**/

[selfGCD];

}

/**********线程Thread操作************/

- (void)thread {

_imageView= [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,100,100)];

[self.viewaddSubview:_imageView];

UIButton*button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(50,500,220,25);

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

//添加方法

[buttonaddTarget:selfaction:@selector(threadClick)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:button];

}

- (void)threadClick {

[NSThreaddetachNewThreadSelector:@selector(threadRun)toTarget:selfwithObject:nil];

}

- (void)threadRun {

NSLog(@"callMainbefore_1");

NSURL*url=[NSURLURLWithString:self.urlArr.firstObject];

NSData*data=[NSDatadataWithContentsOfURL:url];

NSLog(@"callMainbefore_2");

/*

直到updateUI:做完了是否才开始往下操作

waitUntilDone:NO

2016-06-02 17:13:47.618多线程处理[10042:208753] callMainbefore_1

2016-06-02 17:13:47.673多线程处理[10042:208753] callMainbefore_2

2016-06-02 17:13:47.674多线程处理[10042:208753] callMainAfter

2016-06-02 17:13:52.675多线程处理[10042:208668] callMain

waitUntilDone:YES

2016-06-02 17:16:05.525多线程处理[10062:210446] callMainbefore_1

2016-06-02 17:16:05.607多线程处理[10062:210446] callMainbefore_2

2016-06-02 17:16:10.609多线程处理[10062:210347] callMain

2016-06-02 17:16:10.613多线程处理[10062:210446] callMainAfter

**/

[selfperformSelectorOnMainThread:@selector(updateUI:)withObject:datawaitUntilDone:YES];

NSLog(@"callMainAfter");

}

- (void)updateUI:(NSData*)imageData {

sleep(5);

NSLog(@"callMain");

self.imageView.image= [UIImageimageWithData:imageData];

}

/**********线程Thread结束************/

/**********队列操作************/

- (void)operation {

CGFloatspace =10;

CGFloatimageWH = (320-10*5)/4.0;

for(NSIntegeri =0; i

UIImageView* imageView = [[UIImageViewalloc]init];

[self.viewaddSubview:imageView];

imageView.frame=CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);

imageView.tag=AddTag+ i;

}

UIButton*button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(50,500,220,25);

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

[buttonaddTarget:selfaction:@selector(operationClick)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:button];

}

- (void)operationClick {

NSOperationQueue* queue = [[NSOperationQueuealloc]init];

queue.maxConcurrentOperationCount=2;

__weaktypeof(self)weakSelf =self;

for(NSIntegeri =0; i

UIImageView* imageView = [self.viewviewWithTag:AddTag+i];

imageView.image=nil;

NSBlockOperation* blockOp = [NSBlockOperationblockOperationWithBlock:^{

NSURL*url=[NSURLURLWithString:weakSelf.urlArr[i]];

NSData*data=[NSDatadataWithContentsOfURL:url];

dispatch_async(dispatch_get_main_queue(), ^{

//更新UI需要主线程

imageView.image= [UIImageimageWithData:data];

});

}];

if(i >0) {

/*就是依赖前面的**/

NSBlockOperation* preOperation = queue.operations[i-1];

[blockOpaddDependency:preOperation];

}

[queueaddOperation:blockOp];

}

}

/**********队列操作结束************/

/****************** GCD ******************/

- (void)GCD {

CGFloatspace =10;

CGFloatimageWH = (320-10*5)/4.0;

for(NSIntegeri =0; i

UIImageView* imageView = [[UIImageViewalloc]init];

[self.viewaddSubview:imageView];

imageView.frame=CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);

imageView.tag=AddTag+ i;

}

UIButton*button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(50,500,220,25);

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

//添加方法

[buttonaddTarget:selfaction:@selector(GCDClick)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:button];

}

/*串行执行**/

- (void)GCDClick {

__weaktypeof(self)weakSelf =self;

dispatch_queue_tserialQueue =dispatch_queue_create("LeeGCDQueue",DISPATCH_QUEUE_SERIAL);

for(NSIntegeri =0; i

UIImageView* imageView = [self.viewviewWithTag:AddTag+i];

imageView.image=nil;

dispatch_async(serialQueue, ^{

NSURL*url=[NSURLURLWithString:weakSelf.urlArr[i]];

NSData*data=[NSDatadataWithContentsOfURL:url];

dispatch_async(dispatch_get_main_queue(), ^{

//更新UI需要主线程

imageView.image= [UIImageimageWithData:data];

});

});

}

}

///*并发执行**/

//- (void)GCDClick {

//

//__weak typeof(self)weakSelf = self;

//dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//for (NSInteger i = 0 ; i < self.urlArr.count; i++) {

//UIImageView * imageView = [self.view viewWithTag:AddTag+i];

//imageView.image = nil;

//dispatch_async(globalQueue, ^{

//

//NSURL *url=[NSURL URLWithString:weakSelf.urlArr[i]];

//NSData *data=[NSData dataWithContentsOfURL:url];

//dispatch_async(dispatch_get_main_queue(), ^{

//

////更新UI需要主线程

//imageView.image = [UIImage imageWithData:data];

//});

//

//

//});

//

//}

//

//}

/****************** GCD结束******************/

@end

上一篇 下一篇

猜你喜欢

热点阅读