- (void)loadView
{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, Screen_Width, Screen_Height)];
UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
downloadBtn.frame = CGRectMake(20.0f, StatusBar_Height + 80.0f, 200.0f, 40.0f);
downloadBtn.backgroundColor = OrangeColor;
[downloadBtn setTitle:@"下载"
forState:UIControlStateNormal];
[downloadBtn setTitle:@"下载"
forState:UIControlStateSelected];
[downloadBtn addTarget:self
action:@selector(download:)
forControlEvents:UIControlEventTouchUpInside];
[backgroundView addSubview:downloadBtn];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 140.0f, 280.0f, 300.0f)];
[backgroundView addSubview:self.imageView];
self.view = backgroundView;
}
- (void)download:(UIButton*)sender
{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSThread *thread = [[NSThread alloc] initWithTarget:self
selector:@selector(downloadImage:)
object:url];
[thread start];
}
- (void)downloadImage:(NSURL*)url
{
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(updateUI:)
withObject:image
waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage*)image
{
self.imageView.image = image;
}
NSOperation的实现方式:
- (void)download:(UIButton*)sender
{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(downloadImage:)
object:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
- (void)downloadImage:(NSURL*)url
{
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:@selector(updateUI:)
withObject:image
waitUntilDone:YES];
}
}
- (void)updateUI:(UIImage*)image
{
self.imageView.image = image;
}
GCD的实现方式:
- (void)download:(UIButton*)sender
{
//异步更新
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://img2.bdstatic.com/img/image/43064380cd7912397ddcec457455b82b2b7d0a28736.jpg"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
if (data) {
UIImage *image = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
}
});
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
//
// });
//dispatch_group_t 分组,将所有分组中的任务执行后,再更新界面
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"任务一");
});
dispatch_group_async(group, queue, ^{
NSLog(@"任务二");
});
dispatch_group_async(group, queue, ^{
NSLog(@"任务三");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//监测三组任务是否完全完成,完成后更新界面
});
//dispatch_barrier_async 表示前面的任务执行后,再执行dispatch_barrier_async 任务
dispatch_queue_t queue1 = dispatch_queue_create("com.liusanchun.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue1, ^{
NSLog(@"任务一");
});
dispatch_async(queue1, ^{
NSLog(@"任务二");
});
dispatch_barrier_async(queue1, ^{
NSLog(@"执行");
});
dispatch_async(queue1, ^{
NSLog(@"任务三");
});
}