IOS 自帶地圖應用_路徑導航

//網絡

//  ViewController.m多線程

//  NSThreadide

//spa

//  Created by dc008 on 15/12/24..net

//  Copyright © 2015 崔曉宇. All rights reserved.線程

//3d


#import "ViewController.h"orm


@interface ViewController ()對象

{    //用於顯示下載的圖片圖片

    UIImageView *_imageView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self layout];

}


- (void)layout{

    

    _imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    _imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self.view addSubview:_imageView];

    

    //下載按鈕

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(120, 420, 80, 20)];

    [button setTitle:@"加載圖片" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];

    [self.view addSubview:button];

}


- (void)noNSThread{

    //在資源下載加載的過程當中,因爲網絡緣由,有時候咱們是很難保證下載時間的,若是不使用多線程可能用戶完成一個下載操做須要很長時間的等待,並且在這個過程當中沒法進行其餘操做-》阻塞。

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img2.3lian.com/2014/f7/5/d/22.jpg"]]];

    _imageView.image = image;

}


#pragma mark 多線程下載圖片

- (void)loadImageWithMultiThread{

    //方法1:使用對象方法

    //建立一個線程

    NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage) object:nil];

    [thread1 start];//啓動一個線程,並不表明當即執行,而是處於就緒狀態,當系統調度時才真正執行

    

    //方法2.使用類方法

//    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];

}


- (void)loadImage{

    //請求圖片數據

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img2.3lian.com/2014/f7/5/d/22.jpg"]];

    //注意只能在主線程中更新UI

    //performSelectorOnMainThread 這個方法是NSObject的分類方法,每一個NSObject對象都有這個方法

    [self performSelectorOnMainThread:@selector(updataImageView:) withObject:data waitUntilDone:YES];

}


- (void)updataImageView : (NSData *)imageData{

    UIImage *image = [UIImage imageWithData:imageData];

    _imageView.image = image;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索