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/p_w_picpath/w%3D310/sign=897718c6bd096b63811958513c328733/ac345982b2b7d0a28fc5db74c8ef76094a369acf.jpg

   */

        // NSLog(@"1");

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

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

        // _imgView.p_w_picpath = [UIImage p_w_picpathWithData:data];

        

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

        

    }

    

}

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

    

}

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

    _imgView.p_w_picpath = [UIImage p_w_picpathWithData: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);

//    }

//}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章