zoukankan      html  css  js  c++  java
  • iOS多线程开发小demo5 线程间的通信

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

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

  • 相关阅读:
    mysql5.6.20安装
    唯一识别Windows机器的最佳方法
    不用Root在安卓手机上运行Kali_Linux
    在Windows 7和10上显示上次登录帐户信息
    优化非活动窗口的颜色
    Windows10中以管理员身份打开命令提示符
    在Windows10中更改”WIN+E“快捷键打开目标
    在任务管理器中显示所有CPU内核性能
    Windows启动控制台登录模式
    Fluent Terminal
  • 原文地址:https://www.cnblogs.com/dyf520/p/3805308.html
Copyright © 2011-2022 走看看