Vickate_多线程的小方法以及如何使用多线程解决同步请求图片的卡顿现象
2015-09-22 20:29
567 查看
@interface RootViewController () @property (nonatomic, retain) UIImageView *imageV; @property (nonatomic, retain) UIImageView *iamgeView; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addSubview]; [self addImageView]; } // 点击按钮播放一组图片 - (void)addSubview { self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 175, 265)]; _imageV.backgroundColor = [UIColor orangeColor]; [self.view addSubview:_imageV]; [_imageV release]; UIButton *button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; button.frame = CGRectMake(100, 390, 50, 50); button.backgroundColor = [UIColor orangeColor]; [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button]; NSMutableArray *array = [NSMutableArray array]; for (int i = 110; i < 114; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]; [array addObject:image]; } self.imageV.animationDuration = 0.3; self.imageV.animationImages = array; self.imageV.animationRepeatCount = 0; UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; button2.frame = CGRectMake(225, 390, 50, 50); button2.backgroundColor = [UIColor orangeColor]; [button2 addTarget:self action:@selector(actionButton2:) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:button2]; #warning NSThread ---- // 设置多线程解决 既可以执行代码,又可以播放动画 // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(actionButton2:) object:nil]; // [thread start]; #warning NSObject --- // 基类解决卡顿现象 // [self performSelectorInBackground:@selector(actionButton2:) withObject:nil]; // NSOperation也是一个抽象类 没有实现具体的功能 // NSBlockOperation 在block中操作 (相当于任务) // NSInvocationOperation 调用操作 (相当于任务) // NSOperationQueue 线程队列 #warning 不是主线程的方法 // 创建一个 NSInvocationOperation任务 NSInvocationOperation *invocationOP = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(actionInvocation) object:nil]; // 创建一个block任务 NSBlockOperation *blockOP = [NSBlockOperation blockOperationWithBlock:^{ [self block]; }]; // 创建一个队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:invocationOP]; [queue addOperation:blockOP]; } - (void)actionInvocation { NSLog(@"%@ 是不是主线程%d",[NSThread currentThread],[NSThread isMainThread]); } - (void)block { NSLog(@"%@ 是不是主线程%d", [NSThread currentThread],[NSThread isMainThread]); } - (void)actionButton:(UIButton *)button { if (self.imageV.isAnimating == NO) { [self.imageV startAnimating]; } else { [self.imageV stopAnimating]; } } - (void)actionButton2:(UIButton *)button { @autoreleasepool { for (int i = 0; i < 10000000; i++) { NSLog(@"%d", i); } } } #warning 需求 进行同步请求一张图片 不产生屏幕卡顿 // 思路: 开启一个子线程 进行同步请求图片 图片到手后 把图片返回主线程 显示到IMageView上 - (void)addImageView { // 创建一个iamgeView 用于显示图片 self.iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 500, 275, 100)]; _iamgeView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:_iamgeView]; [_iamgeView release]; #warning 网络请求 // 创建一个任务 NSBlockOperation *blockOPr = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool { NSURL *url = [NSURL URLWithString:@"http://img3.cache.netease.com/photo/0026/2013-03-19/8QATT85A4CJ80026.jpg"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"GET"]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; [self performSelectorInBackground:@selector(actionImage:) withObject:data]; } }]; // 创建一个队列 NSOperationQueue *queue1 = [[NSOperationQueue alloc] init]; [queue1 addOperation:blockOPr]; } - (void)actionImage:(NSData *)data { _iamgeView.image = [UIImage imageWithData:data]; } 欢迎光顾iOS_Bay的博客,谢谢!
相关文章推荐
- Light oj 1095 - Arrange the Numbers(错排)
- Java中HashMap源码解析
- 团队作业2
- 寻找hash值——把int array看成是一个整数
- unity3d 学习笔记四 skybox(天空盒) light(光源) halo(光晕)
- linux下 eclipse 集成环境开发 Servlet 之 HelloWorld
- scala 函数参数 变长参数 类继承测试
- C-Free 您不能使用调试解决方案
- JAVA问题总结之22--Arraylist集合的操作
- IOS中延时执行的几种方式的比较和汇总
- HDU 5451 Best Solver(fibonacci)
- C++学习记录之string字符串
- java第四周作业——3.22
- HDOJ 1076 An Easy Task (第n个闰年)
- HDU 5238 Calculator【线段树】
- 数据结构(线段树)训练
- iPhone变成白苹果,苹果手机变成白苹果怎么办
- 团队介绍——窝窝头
- hdu 2149Public Sale(博弈论 巴什博奕)
- C语言程序初体验-第六课-第三题:一周工资数