1. 程式人生 > >iOS執行緒通訊

iOS執行緒通訊

1、執行緒通訊的概念

     1)一個執行緒傳遞資料給另一個執行緒;

     2)在一個執行緒中執行完特定任務後,轉到另一個執行緒繼續執行任務;

2、例項

     圖片下載顯示:

     - (IBAction)add:(id)sender {

    [self performSelectorInBackground:@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=[NSData

dataWithContentsOfURL:[NSURL URLWithString:urlStr]];

    UIImage*image=[UIImage imageWithData:data];

    

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

   }

   -(void)downLoadDone:(

UIImage*)image

   {

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

    self.imageView.image=image;

   }