iOS线程通信

来源:互联网 发布:java 日志级别 编辑:程序博客网 时间:2024/05/22 01:30

1、线程通信的概念

     1)一个线程传递数据给另一个线程;

     2)在一个线程中执行完特定任务后,转到另一个线程继续执行任务;

2、实例

     图片下载显示:

     - (IBAction)add:(id)sender {

    [selfperformSelectorInBackground:@selector(downLoad)withObject:nil];

     }

     - (IBAction)delet:(id)sender {

    self.imageView.image=nil;

    }

    -(void)downLoad

   {

    NSString*urlStr=@"http://www.0739i.com.cn/data/attachment/portal/201603/09/120156l1yzzn747ji77ugx.jpg";

    NSData*data=[NSDatadataWithContentsOfURL:[NSURLURLWithString:urlStr]];

    UIImage*image=[UIImageimageWithData:data];

    

    [selfperformSelectorOnMainThread:@selector(downLoadDone:)withObject:image waitUntilDone:NO];

   }

   -(void)downLoadDone:(UIImage*)image

   {

     NSLog(@"currentThread2=%@",[NSThreadcurrentThread]);

    self.imageView.image=image;

   }

     

0 0