溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

UI中多線程的創(chuàng)建以及定時(shí)器與圖片的異步加載

發(fā)布時(shí)間:2020-07-11 00:16:53 來源:網(wǎng)絡(luò) 閱讀:311 作者:ladispartion1 欄目:開發(fā)技術(shù)


- (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是一個(gè)操作隊(duì)列或者線程池

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

     // queue.maxConcurrentOperationCount = 1;

//    [queue addOperationWithBlock:^{//往隊(duì)列中添加一個(gè)操作

//        [self test];

//    }];

    

//    //方法5 不要直接去創(chuàng)建NSOperation對(duì)象

    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];

    

  /*

   https://cache.yisu.com/upload/information/20200312/67/250146.jpg

   */

        // NSLog(@"1");

        NSString *imgUrl = @"https://cache.yisu.com/upload/information/20200312/67/250147.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);

//    }

//}



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI