ios学习(十九)NSThread简单使用

#import "ViewController.h"

@interface ViewController ()
-(void)printMuch;
@property (weak, nonatomic) IBOutlet UIImageView *picImg;


@end

@implementation ViewController
- (IBAction)buttonClicked:(id)sender {
//    [self printMuch];
//    [self createThread1];
//    [self createThread2];
//    [self createThread3];
//    [self downloadImage];
    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
    
}

-(void)downloadImage{
    NSDate *begin = [NSDate date];
    NSURL *url = [NSURL URLWithString:@"https://a-ssl.duitang.com/uploads/item/201504/03/20150403H4808_2QkBX.jpeg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data scale:2];
    NSDate *end = [NSDate date];
    
    
    //通知主线程更新ui
//    [self performSelectorOnMainThread:@selector(setDataToImageView:) withObject:image waitUntilDone:YES];
    [self performSelector:@selector(setDataToImageView:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    
    NSLog(@"wo zai deng dai");
    NSLog(@"%f", [end timeIntervalSinceDate:begin]);
    
}

-(void)setDataToImageView:(UIImage *)image{
    [self.picImg setImage:image];
}

-(void) createThread1{
    NSThread *tread = [[NSThread alloc] initWithTarget:self selector:@selector(printMuch) object:nil];
    [tread start];
}

-(void)createThread2{
    [NSThread detachNewThreadSelector:@selector(printMuch) toTarget:self withObject:nil];
}

-(void)createThread3{
    [self performSelectorInBackground:@selector(printMuch) withObject:nil];
}

-(void)printMuch{
    for (NSInteger i = 0; i < 10000; i++){
        NSLog(@"%ld", (long)i);
    }
}


- (void)viewDidLoad {
    [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.
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值