标签:
@implementaion FKViewController
-(void) viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)showImage:(id)sender
{
NSString* url = @"http://www.crazyit.ortg/logo.jpg";
// 创建新线程对象
NSThread *thread = [NSThread alloc] initWithTarget:self selector:@selector[downloadImageFromURL:) object:url];
// 启动线程
[thread start];
}
//定义一个方法作为线程执行体
-(void)downloadImageFromURL:(NSString *) url
{
// 从网络获取数据
NSData *data = [[NSData alloc] initWithContentOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc] initWithData:data];
if (image != nil)
{
// 在主线程中执行updateUI:方法
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
}
else
{
NSLog(@"------ 下载图片出现错误-----");
}
}
-(void)updateUI:(UIImage*) image
{
self.iv.image = image;
}
标签:
原文地址:http://blog.csdn.net/zc707212993/article/details/42984281