IOS-NSThread-下載圖片

//
//  ViewController.m
//  demo
//
//  Created by yangjing on 2019/3/5.
//  Copyright © 2019年 yangjing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)loadView {
    
    self.scrollView =[[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.view = self.scrollView;
    self.imageView = [[UIImageView alloc] init];
    [self.scrollView addSubview:self.imageView];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
    [thread start];
   
}

-(void)downloadImage {
    
    NSURL *url = [NSURL URLWithString:@"http://pic9.nipic.com/20100824/2531170_082435310724_2.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:data];
    
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:img waitUntilDone:YES];
    
}

- (void)updateUI:(UIImage *)img{
    
    self.imageView.image = img;
    [self.imageView sizeToFit];
    self.scrollView.contentSize = img.size;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end



複製代碼
相關文章
相關標籤/搜索