#import "ViewController.h"
static NSInteger const AddTag = 100;
@interface ViewController ()
@property (nonatomic,strong) UIImageView * imageView;
@property (nonatomic,strong) NSArray * urlArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
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 GCD];
}
- (void)thread {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:_imageView];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(50, 500, 220, 25);
[button setTitle:@"加载图片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(threadClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)threadClick {
[NSThread detachNewThreadSelector:@selector(threadRun) toTarget:self withObject:nil];
}
- (void)threadRun {
NSLog(@"callMainbefore_1");
NSURL *url=[NSURL URLWithString:self.urlArr.firstObject];
NSData *data=[NSData dataWithContentsOfURL:url];
NSLog(@"callMainbefore_2");
[self performSelectorOnMainThread:@selector(updateUI:) withObject:data waitUntilDone:YES];
NSLog(@"callMainAfter");
}
- (void)updateUI:(NSData *)imageData {
sleep(5);
NSLog(@"callMain");
self.imageView.image = [UIImage imageWithData:imageData];
}
- (void)operation {
CGFloat space = 10;
CGFloat imageWH = (320 - 10*5)/4.0;
for (NSInteger i = 0 ; i < self.urlArr.count; i++) {
UIImageView * imageView = [[UIImageView alloc]init];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);
imageView.tag = AddTag + i;
}
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(50, 500, 220, 25);
[button setTitle:@"加载图片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(operationClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)operationClick {
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = 2;
__weak typeof(self)weakSelf = self;
for (NSInteger i = 0 ; i < self.urlArr.count; i++) {
UIImageView * imageView = [self.view viewWithTag:AddTag+i];
imageView.image = nil;
NSBlockOperation * blockOp = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url=[NSURL URLWithString:weakSelf.urlArr[i]];
NSData *data=[NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:data];
});
}];
if (i >0) {
NSBlockOperation * preOperation = queue.operations[i-1];
[blockOp addDependency:preOperation];
}
[queue addOperation:blockOp];
}
}
- (void)GCD {
CGFloat space = 10;
CGFloat imageWH = (320 - 10*5)/4.0;
for (NSInteger i = 0 ; i < self.urlArr.count; i++) {
UIImageView * imageView = [[UIImageView alloc]init];
[self.view addSubview:imageView];
imageView.frame = CGRectMake(space +(space+imageWH)*(i%4), space +(space+imageWH)*(i/4), imageWH, imageWH);
imageView.tag = AddTag + i;
}
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(50, 500, 220, 25);
[button setTitle:@"加载图片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(GCDClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)GCDClick {
__weak typeof(self)weakSelf = self;
dispatch_queue_t serialQueue = dispatch_queue_create("LeeGCDQueue", DISPATCH_QUEUE_SERIAL);
for (NSInteger i = 0 ; i < self.urlArr.count; i++) {
UIImageView * imageView = [self.view viewWithTag:AddTag+i];
imageView.image = nil;
dispatch_async(serialQueue, ^{
NSURL *url=[NSURL URLWithString:weakSelf.urlArr[i]];
NSData *data=[NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:data];
});
});
}
}
@end