03、NSThread

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//方式1
//NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo) object:nil];
//[thread start];
//方式2
//[NSThread detachNewThreadSelector:@selector(demo) toTarget:self withObject:nil];
//方式3
//[self performSelectorInBackground:@selector(demo) withObject:nil];
//方式4 参数
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(demo:) object:@"蒋卫生"];
[thread start];
}

- (void)demo:(NSString *)name {
NSLog(@"hello %@",name);
}

//- (void)demo {
// NSLog(@"hello %@",[NSThread currentThread]);
//}


----------------------------图片下载案例----------------------------
在plist文件中加入网络权限
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
--------------------------图片下载案例代码--------------------------
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) IBOutlet UILabel *lbl;
@end

@implementation ViewController

- (void)loadView {
//初始化scrollview
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView.backgroundColor = [UIColor whiteColor];
self.view = self.scrollView;
//初始化imageView
self.imageView = [[UIImageView alloc] init];
[self.scrollView addSubview:self.imageView];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
[thread start];
}

//下载网络图片
- (void)downloadImage {
//图片的地址
NSURL *url = [NSURL URLWithString:@"http://img04.tooopen.com/images/20130701/tooopen_20083555.jpg"];
//下载图片
NSData *data = [NSData dataWithContentsOfURL:url];
//把NSData转换成UIImage
UIImage *img = [UIImage imageWithData:data];
//在主线程上更新UI控件 线程间通信
//waitUntilDone 值是YES 会等待方法之行完毕,才会执行后续代码
[self performSelectorOnMainThread:@selector(updateUI:) withObject:img waitUntilDone:YES];
}

- (void)updateUI:(UIImage *)img {
self.imageView.image = img;
//让imageview的大小和图片的大小一致
[self.imageView sizeToFit];
//设置scrollView滚动范围
self.scrollView.contentSize = img.size;
}

@end




猜你喜欢

转载自blog.csdn.net/daidaishuiping/article/details/80258570