UI中多线程的创建以及定时器与图片的异步加载


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];

    _imgView.contentModeUIViewContentModeScaleAspectFit;

    [self.view addSubview:_imgView];

    

    

    // Do any additional setup after loading the view, typically from a nib.

    

//    //方法1

//    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];

//    [thread start];

//

//    //方法2

//    [self performSelectorInBackground:@selector(test) withObject:nil];

    

//      //方法3

//    [NSThread detachNewThreadSelector:@selector(test) toTarget:self withObject:nil];

    

//    //方法4 NSOperationQueue是一个操作队列或者线程池

      NSOperationQueue *queue = [[NSOperationQueue alloc]init];

     // queue.maxConcurrentOperationCount = 1;

//    [queue addOperationWithBlock:^{//往队列中添加一个操作

//        [self test];

//    }];

    

//    //方法5 不要直接去创建NSOperation对象

    NSInvocationOperation *opertion = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];

//    opertion.queuePriority = NSOperationQueuePriorityVeryLow;

//    

//    NSInvocationOperation *opertion2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];

//    opertion2.queuePriority = NSOperationQueuePriorityVeryHigh;

//   

    //方法6

//    ThreadOperation *threadOperation = [[ThreadOperation alloc]init];

//    [queue addOperation:threadOperation];

    

    [queue addOperation:opertion];

//    [queue addOperation:opertion2];


    

    

//    BOOL bool1 = [[NSThread currentThread]isMainThread];

//    NSLog(@"bool1 is %d",bool1);

//    for (int i = 0; i < 10; i ++) {

//        NSLog(@"-----------------main%d",i);

//    }

    

}

- (void)test{

    

      @autoreleasepool {

   // [NSThread sleepForTimeInterval:5];

//    BOOL bool2 = [[NSThread currentThread]isMainThread];

//    NSLog(@"bool2 is %d",bool2);

//    for (int i = 0; i < 10; i ++) {

//        NSLog(@"-----------------test%d",i);

//    }

    

//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(run:) userInfo:nil repeats:YES];


//    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(run:) userInfo:nil repeats:YES];

//    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

//    

//    [[NSRunLoop currentRunLoop]run];

    

  /*

   http://d.hiphotos.baidu.com/image/w%3D310/sign=897718c6bd096b63811958513c328733/ac345982b2b7d0a28fc5db74c8ef76094a369acf.jpg

   */

        // NSLog(@"1");

        NSString *imgUrl = @"http://g.hiphotos.baidu.com/image/w%3D310/sign=2c97ee80d03f8794d3ff4e2fe21a0ead/f636afc379310a550dcbd3ccb34543a98226101e.jpg";

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];

        // _imgView.image = [UIImage imageWithData:data];

        

        [self performSelectorOnMainThread:@selector(show:) withObject:data waitUntilDone:NO];

        

    }

    

}

- (void)run:(NSTimer *)timer{

    

}

- (void)show:(NSData *)data{

    _imgView.image = [UIImage imageWithData:data];

}

//- (void)test1{

//    //[NSThread sleepForTimeInterval:5];

//    BOOL bool2 = [[NSThread currentThread]isMainThread];

//    NSLog(@"bool2 is %d",bool2);

//    for (int i = 0; i < 10; i ++) {

//        NSLog(@"-----------------test111111%d",i);

//    }

//}



你可能感兴趣的:(多线程,定时器,图片)