码迷,mamicode.com
首页 > 移动开发 > 详细

iOS多线程开发小demo5 线程间的通信

时间:2014-06-24 11:18:23      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:des   class   blog   http   com   2014   

//  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

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

iOS多线程开发小demo5 线程间的通信,布布扣,bubuko.com

iOS多线程开发小demo5 线程间的通信

标签:des   class   blog   http   com   2014   

原文地址:http://www.cnblogs.com/dyf520/p/3805308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!