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

发布时间:2020-07-11 00:16:53 作者:ladispartion1
来源:网络 阅读:313


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

    

  /*

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

//    }

//}



推荐阅读:
  1. UI中CollectionView的创建与使用
  2. 如何实现多线程-线程的创建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

图片 多线程 定时器

上一篇:一键安装dhcp服务脚本

下一篇:ebs 根据一个用户的创建另外一个用户并添加职责

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》