zoukankan      html  css  js  c++  java
  • GCD多线程 在子线程中获取网络图片 在主线程更新

    子线程中得所有数据都可以直接拿到主线程中使用
    //当触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
         //获取一个全局并行队列
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         //把任务添加到队列中执行
         dispatch_async(queue, ^{
             //打印当前线程
             NSLog(@"%@",[NSThread currentThread]);
           //从网络上下载图片
             NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
             NSData *data=[NSData dataWithContentsOfURL:urlstr];
    UIImage *image=[UIImage imageWithData:data]; //提示 NSLog(@"图片加载完毕"); dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image=image; //打印当前线程 NSLog(@"%@",[NSThread currentThread]); }); }); }
     
  • 相关阅读:
    17多校6 HDU
    E. Present for Vitalik the Philatelist 反演+容斥
    HDU
    F. Cowmpany Cowmpensation dp+拉格朗日插值
    hdu6088 组合数+反演+拆系数fft
    任意模数fft
    Codeforces Round #258 (Div. 2)E
    bzoj3670: [Noi2014]动物园
    HDU
    IO-InputStreamReader
  • 原文地址:https://www.cnblogs.com/zeroChan/p/4968915.html
Copyright © 2011-2022 走看看