iOS--线程间通讯NSThread--04
来源:互联网 发布:信息科技公司 域名 编辑:程序博客网 时间:2024/06/06 09:27
1.回到主线程,刷新UI界面(为了线程安全)
#import "HMViewController.h"@interface HMViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation HMViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self performSelectorInBackground:@selector(download) withObject:nil];}/** * 下载图片 */- (void)download{ NSLog(@"download---%@", [NSThread currentThread]); // 1.图片地址 NSString *urlStr = @"http://d.hiphotos.baidu.com/image/pic/item/37d3d539b6003af3290eaf5d362ac65c1038b652.jpg"; NSURL *url = [NSURL URLWithString:urlStr]; // 2.根据地址下载图片的二进制数据(这句代码最耗时) NSLog(@"---begin"); NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"---end"); // 3.设置图片 UIImage *image = [UIImage imageWithData:data]; // 4.回到主线程,刷新UI界面(为了线程安全) [self performSelectorOnMainThread:@selector(downloadFinished:) withObject:image waitUntilDone:NO];// [self performSelector:@selector(downloadFinished:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];// [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES]; NSLog(@"-----done----");}- (void)downloadFinished:(UIImage *)image{ self.imageView.image = image; NSLog(@"downloadFinished---%@", [NSThread currentThread]);}
1 0
- iOS--线程间通讯NSThread--04
- iOS 之多线程 NSThread
- iOS NSThread线程
- IOS 多线程 线程NSThread
- IOS 多线程 线程NSThread
- IOS线程 NSThread
- iOS线程其一NSThread
- iOS线程之NSThread
- iOS中NSThread线程
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- 深入浅出 iOS 之多线程 NSThread
- iOS开发之多线程NSThread
- dialog中的edit无法弹出输入法
- Bottle.py的官方教程翻译译者序
- React Native配置运行官方Example Demo 等等
- 通过ActivityManager 管理activity
- java HashMap 实现原理
- iOS--线程间通讯NSThread--04
- 怎么将一个十六进制数转变成十进制数
- 简单的http服务器java
- Angular 几篇关于指令的好文章收录(一)
- Matlab中低级文件I/O操作函数
- RxJava2 浅析
- crul 的使用
- java——final关键字
- Linux常用命令大全