使用多线程下载网络图片(一)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)downLoadImage:(id)sender {
    NSString* url=@"http://pic36.nipic.com/20131203/3822951_100659752000_2.jpg";
    NSThread* th=[[NSThread alloc]initWithTarget:self selector:@selector(downLoadImageFromURL:) object:url];
    [th start];
}
-(void)downLoadImageFromURL:(NSString*)url
{
    NSData* data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
    UIImage* image=[[UIImage alloc]initWithData:data];
    if (image!=nil) {
        [self performSelectorOnMainThread:@selector(getImage:) withObject:image waitUntilDone:YES];
    }
    else
    {
        NSLog(@"下载出错!");
    }
}
-(void)getImage:(UIImage*)image
{
    self.iv.image=image;
}
运行之后,效果如图:


你可能感兴趣的:(image,图片)