#import "MyOperation.h"
@implementation MyOperation
-(void )main
{
NSInteger count = 1 ;
for (NSInteger i = 0 ; i<600000000 ; i++) {
count ++;
}
NSLog (@"%ld" ,count);
}
@end
#import "ViewController.h"
#import "MyOperation.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
@interface ViewController ()
@property (nonatomic ,retain)UIButton *button;
@property (nonatomic ,retain)UIImageView *imageView;
@end
@implementation ViewController
- (void )viewDidLoad {
[super viewDidLoad];
self .button = [UIButton buttonWithType:UIButtonTypeSystem];
self .button .frame = CGRectMake(100 , 100 , 150 , 50 );
[self .button setTitle:@"测试" forState:UIControlStateNormal];
self .button .layer .borderWidth = 1 ;
self .button .layer .cornerRadius = 10 ;
[self .view addSubview:self .button ];
[self .button addTarget:self action:@selector (GCDAction:) forControlEvents:UIControlEventTouchUpInside];
self .imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100 , 200 , 150 , 150 )];
self .imageView .backgroundColor = [UIColor cyanColor];
[self .view addSubview:self .imageView ];
[_imageView release];
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];
self .imageView .image =image;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://help.adobe.com/archive/en/photoshop/cs6/photoshop_reference.pdf" ]];
AFHTTPRequestOperation *
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES );
NSString *cachesPath =sandBox[0 ];
NSString *pdfPath = [cachesPath stringByAppendingPathComponent:@"test.pdf" ];
NSLog (@"%@" ,pdfPath);
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:pdfPath append:NO ];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}
-(void )click:(UIButton *)button
{
NSInteger count = 0 ;
for (NSInteger i = 0 ; i<100000 ; i++) {
count++;
NSLog (@"%ld" ,count);
}
NSLog (@"%ld" ,count);
}
#pragma mark 第一种解决线程问题的方法 NSObject提供的方法
-(void )NSObjectThread:(UIButton *)button
{
[self performSelectorInBackground:@selector (click:) withObject:button];
}
#pragma mark 第二种方式 NSThread
-(void )NSTHreadAction:(UIButton *)button
{
NSLog (@"%@" ,[NSThread currentThread]);
[NSThread sleepForTimeInterval:3 ];
NSLog (@"111111" );
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector (click:) object:nil ];
thread.name = @"zhangyangyang" ;
[thread start];
}
#pragma mark 第三种 NSOperation 任务
-(void )operationAtion:(UIButton *)button
{
MyOperation *operation = [[MyOperation alloc] init];
[operation start];
}
-(void )operationQueue:(UIButton *)button
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:2 ];
MyOperation *op1 = [[MyOperation alloc] init];
MyOperation *op2 = [[MyOperation alloc] init];
MyOperation *op3 = [[MyOperation alloc] init];
MyOperation *op4 = [[MyOperation alloc] init];
MyOperation *op5 = [[MyOperation alloc] init];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
[queue addOperation:op5];
}
-(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, ^{
NSString *picStr = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg" ;
NSURL *url =[NSURL URLWithString:picStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async (mainQueue, ^{
self .imageView .image = image;
});
});
}
- (void )didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end