2010-05-11 71 views
2

我尝试在iphone应用中显示图像,但未显示。我在IB连接,我检查显示本地图像,它运作良好。我也检查从图像的网址,这是可以的。我用下面的代码:在uiimageview中显示来自url的图像

NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail]; 
NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
UIImage *image = [UIImage imageWithData:imageData]; 
imageView.image = image; 

//Video.h 
NSString *urlThumbnail; 

,这是我的视图代码。

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [tableView reloadData]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSLog(@"URLTHUMBNAIL: %@", aVideo.urlThumbnail); 
    NSURL *imageURL = [NSURL URLWithString: aVideo.urlThumbnail]; 
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
    UIImage *image = [UIImage imageWithData:imageData]; 
    imageView.image = image; 
} 

的NSLog上控制台getts

URLTHUMBNAIL: http://147.83.74.180/thumbnails/56.jpg 

上IB的连接是好的,因为我可以使用在相同的IBAction为下一个代码显示的局部图像

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"barret" ofType:@"png"]]; 
+2

代码看起来不错。再次检查您的IBoutlets,或发布更多代码。 – Jordan 2010-05-11 22:39:56

+0

IBoutlet没问题。我在ViewDidAppear上写这个。 – nabrugir 2010-05-11 22:45:32

回答

1

它可以是非常对自己有帮助NSLog()一些关于变量中的真实情况的说明。如果你真的有一切正确的IB联系,可能是aVideo.urlThumbnail没有设置。我希望在使用它创建NSURL之前在控制台中看到该值。

+0

我在这个问题上添加了测试。 – nabrugir 2010-05-12 00:22:56

0

首先,由于NSData dataWithContentsOfURL:访问网络,您应该考虑在后台执行此操作。否则,您的用户界面可能无法响应。

您应该考虑添加一些额外的日志记录来检查imageDataimage的值。此外,您可以致电NSData dataWithContentsOfURL:options:error:查看是否是失败的根源。

相关问题