ViewController.m
#import "ViewController.h"
#import "MyOperation.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
@interface ViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation ViewController
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 200, 200)];
self.imageView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.imageView];
[_imageView release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"测试" forState:UIControlStateNormal];
[button addTarget:self action:@selector(GCDAction:) forControlEvents:UIControlEventTouchUpInside];
- (void)clickAction:(UIButton *)button {
NSInteger count = 0;
for (NSInteger i = 0; i < 9999999999; i++) {
count ++;
NSLog(@"%ld", count);
}
}
- (void)NSObjectAction:(UIButton *)button {
[self performSelectorInBackground:@selector(clickAction:) withObject:button];
}
- (void)NSThreadAction:(UIButton *)button {
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(clickAction:) object:nil];
thread.name = @"student";
[thread start];
}
- (void)NSOperationAction:(UIButton *)button {
MyOperation *operation = [[MyOperation alloc] init];
[operation start];
}
- (void)queueAction:(UIButton *)button {
MyOperation *operation1 = [[MyOperation alloc] init];
MyOperation *operation2 = [[MyOperation alloc] init];
MyOperation *operation3 = [[MyOperation alloc] init];
MyOperation *operation4 = [[MyOperation alloc] init];
MyOperation *operation5 = [[MyOperation alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
[queue addOperation:operation4];
[queue addOperation:operation5];
}
- (void)GCDAction:(UIButton *)button {
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(globalQueue, ^{
NSURL *url = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(mainQueue, ^{
self.imageView.image = image;
});
});
}