[多线程之二]-NSOperation练习
来源:互联网 发布:学声乐的软件 编辑:程序博客网 时间:2024/06/07 09:24
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (strong, nonatomic) IBOutlet UIImageView *MainImgView;@property (strong, nonatomic) IBOutlet UIImageView *BlockImgView;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { NSURL* url = [[NSURL alloc] initWithString:@"http://avatar.csdn.net/3/0/F/1_liyanq528.jpg"]; //注意1:NSInvocationOperation这个只能传一个参数 //注意2:NSInvocationOperation直接调用的话,是放在主线程调用,只有用queue里面才是其他线程调用。 NSInvocationOperation* operat = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downLoadImg:) object:url]; [operat setCompletionBlock:^{ NSLog(@"MainImgView图像更新完成"); }]; //注意3:NSBlockOperation直接调用只有一个block,是放在主线程调用。有多个block,或者放入queue里面,就可以其他线程调用 NSBlockOperation* bOperat = [NSBlockOperation blockOperationWithBlock:^{ NSData* data = [[NSData alloc] initWithContentsOfURL:url]; if (data) { UIImage* img = [[UIImage alloc] initWithData:data]; NSLog(@"%@",[NSThread currentThread]); dispatch_async(dispatch_get_main_queue(), ^{ self.BlockImgView.image = img; }); //[self performSelectorOnMainThread:@selector(updateImg:) withObject:img waitUntilDone:NO]; } }]; [bOperat setCompletionBlock:^{ NSLog(@"BlockImgView图像更新完成"); }]; NSOperationQueue* que = [[NSOperationQueue alloc]init]; //注意4:并发的线程数量不宜过多,最好5以内,否则会影响ui [que setMaxConcurrentOperationCount:3]; [que addOperation:operat]; [que addOperation:bOperat]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}-(void) updateImg:(UIImage*) aimg{ self.MainImgView.image = aimg;}-(void) downLoadImg:(NSURL*) aurl{ NSData* data = [[NSData alloc] initWithContentsOfURL:aurl]; if (data) { UIImage* img = [[UIImage alloc] initWithData:data]; dispatch_async(dispatch_get_main_queue(), ^{ self.MainImgView.image = img; }); //[self performSelectorOnMainThread:@selector(updateImg:) withObject:img waitUntilDone:NO]; }}@end参考:http://www.cnblogs.com/wendingding/p/3809042.html
0 0
- [多线程之二]-NSOperation练习
- iOS多线程之二 NSOperation
- 多线程之-NSOperation
- iOS多线程学习之NSOperation(二)
- iOS多线程开发系列之(二)NSOperation
- iOS多线程 (二)-----------NSOperation
- 多线程开发之NSOperation
- 多线程 之 NSOperation
- 多线程 之 NSOperation
- iOS多线程之NSOperation
- iOS多线程之NSOperation
- iOS多线程之NSOperation
- ios多线程之NSOperation
- IOS多线程之NSOperation
- 多线程之NSOperation
- 多线程之NSOperation
- 多线程之-NSOperation
- 多线程之NSOperation对象
- IOS学习笔记之UISwitch
- 耿建玲视频总结
- CBIR: Texture Features 基于内容的图像检索:纹理特征
- Arduino - 摇杆模块
- Experimental CBIR Systems 基于内容的图像检索系统
- [多线程之二]-NSOperation练习
- 6月英语
- Calendar用法
- 微信JS-SDK 修改分享标题 分享图片
- JSONArray使用
- mapdb 开源包踩坑小记
- 2015年总结
- SQL Server索引的维护 - 索引碎片、填充因子
- Task4