多线程 线程之间的通信

//  DYFViewController.m
//  623-06-线程间的通信
//
//  Created by dyf on 14-6-23.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
 
#import "DYFViewController.h"
 
@interface DYFViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imaV;
 
@end
 
@implementation DYFViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.下载图片
    [self performSelector:@selector(download) withObject:nil];
     
    // 显示、刷新界面,只能在主界面
    // 2.显示图片
}
/**
 *  下载图片:子线程
 */
- (void)download
{
    // 1.根据URL下载图片
    NSURL *url = [NSURL URLWithString:@"http://image.cache.xiu8.com/live/125/125/997729.jpg"];
    NSLog(@"---------begin");
    NSData *data = [NSData dataWithContentsOfURL:url]; // 耗时
     
    NSLog(@"-----------end");
     
    UIImage *image = [UIImage imageWithData:data];
     
    // 2.回到主线程显示图片
//    [self.imaV performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO modes:nil];
    //[self performSelectorOnMainThread:@selector(settingImage:) withObject:nil waitUntilDone:NO];
    [self.imaV performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
}
/**
 *  显示图片:主线程
 *
 *  @param image image
 */
- (void)settingImage:(UIImage *)image
{
    self.imaV.image = image;
}
 
@end

 小结:主线程与子线程之间的通信

 
 
posted @ 2015-07-21 12:52  Cheetah_yang  阅读(155)  评论(0编辑  收藏  举报