NSOperationQueue(多线程请求队列)
2017-06-22 17:10
369 查看
我的控件都是拖控件的
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageViewTp1; @property (weak, nonatomic) IBOutlet UIImageView *imageViewTp2; @property (weak, nonatomic) IBOutlet UIImageView *imageViewTp3; @property (weak, nonatomic) IBOutlet UIImageView *imageViewTp4; @property (retain)NSMutableArray* urlArr; //创建请求队列 @property (retain)NSOperationQueue* queue; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //开辟空间 _urlArr = [NSMutableArray new]; NSString* urlString1 = @"http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"; NSString* urlString2 = @"http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg"; NSString* urlString3 = @"http://pic11.nipic.com/20101214/213291_155243023914_2.jpg"; NSString* urlString4 = @"http://pic.58pic.com/58pic/14/27/45/71r58PICmDM_1024.jpg"; [_urlArr addObject:urlString1]; [_urlArr addObject:urlString2]; [_urlArr addObject:urlString3]; [_urlArr addObject:urlString4]; } - (IBAction)loadingAction:(UIButton *)sender { //创建请求队列,队列放的是一个个异步请求线程 _queue = [[NSOperationQueue alloc]init]; for (NSInteger i = 0; i < 4; i++) { //创建请求线程 NSArray* arr = @[_urlArr[i],[NSString stringWithFormat:@"%ld", i+10]]; NSInvocationOperation* opertation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:arr]; //把线程加载到队列中,让队列去实现开始和结束 [_queue addOperation:opertation]; } } - (IBAction)suspendedAction:(UIButton *)sender { //取消所有线程的执行 [_queue cancelAllOperations]; } -(void)downloadImage:(NSArray*)arr { NSURL* url = [NSURL URLWithString:arr[0]]; NSData* data = [NSData dataWithContentsOfURL:url]; UIImage* image = [UIImage imageWithData:data]; UIImageView* imageView = [self.view viewWithTag:[arr[1] integerValue]]; imageView.image = image; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- iOS学习笔记-118.多线程17——NSOperationQueue队列的取消、暂停、恢复
- 多线程之NSOperationQueue(线程队列)
- 通过GCD、NSOperationQueue队列、NSThread三种方法来创建多线程
- 使用NSOperation和NSOperationQueue启动多线程
- 多线程编程3 - NSOperationQueue
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 多线程之NSOperation和NSOperationQueue
- iOS 多线程开发之OperationQueue(二)NSOperation VS GCD
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- IOS开发 多线程编程 - NSOperationQueue
- 多线程之-NSOperationQueue
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- Cocoa Touch(四): 多线程GCD, NSObject, NSThread, NSOperationQueue
- 多线程之 NSOperation 与 NSOperationQueue 的基本操作 适合基础学习
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- NSOperationQueue 简化多线程开发