iOS Developer

IOS:OC--图片加载NSThread

2017-06-13  本文已影响0人  任任任任师艳

import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)NSData * imageData;
@end

@implementation ViewController

pragma mark ----创建子线程加载数据

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

[self performSelectorInBackground:@selector(loadimage) withObject:nil];

}
-(void)loadimage{

 NSLog(@"1: %@ \n %d",[NSThread currentThread],[NSThread isMainThread]);

NSString * path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497350942852&di=f2ac74a4d80ee6a0a4024e9e68204495&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201505%2F08%2F20150508173414_uxvTU.jpeg";
NSURL * url = [NSURL URLWithString:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
self.imageData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//进入主线程 刷新UI
[self performSelectorOnMainThread:@selector(reloadimage) withObject:nil waitUntilDone:YES];

}
-(void)reloadimage{
NSLog(@"2: %@ \n%d",[NSThread currentThread],[NSThread isMainThread]);
//imageView 数据显示
self.imageView.image = [UIImage imageWithData:self.imageData];
}

上一篇 下一篇

猜你喜欢

热点阅读