在uiimageview中显示来自url的图像

时间:2023-01-24 18:03:23

I try to show a image in my iphone app but it doesn't show. I connect in IB and I check to show a local image, it workd good. I also check the url from image and it is ok. I use the next code:

我尝试在我的iPhone应用程序中显示图像,但它没有显示。我在IB中连接并且我检查以显示本地图像,它工作得很好。我也从图像中检查了网址,这没关系。我用下一个代码:

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

and

//Video.h
NSString *urlThumbnail;

and this is my code on the view.

这是我在视图上的代码。

- (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 on console

NSLog在控制台上获取

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

The connections on IB are ok, cause I can display a local image using next code on the same IBAction

IB上的连接正常,因为我可以使用同一IBAction上的下一个代码显示本地图像

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

2 个解决方案

#1


1  

It can be very helpful to NSLog() yourself some notes about what's really in your variables. If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. I'd like to see that value in the console right before creating your NSURL with it.

NSLog()自己可以非常有用一些关于变量中真实内容的注释。如果你真的把所有东西都连接在IB上,那么可能是aVideo.urlThumbnail没有被设置。我希望在创建NSURL之前在控制台中看到这个值。

#2


0  

First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. Otherwise, your UI may become unresponsive.

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

You should consider adding some additional logging to check the value of imageData and image. Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure.

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

#1


1  

It can be very helpful to NSLog() yourself some notes about what's really in your variables. If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. I'd like to see that value in the console right before creating your NSURL with it.

NSLog()自己可以非常有用一些关于变量中真实内容的注释。如果你真的把所有东西都连接在IB上,那么可能是aVideo.urlThumbnail没有被设置。我希望在创建NSURL之前在控制台中看到这个值。

#2


0  

First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. Otherwise, your UI may become unresponsive.

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

You should consider adding some additional logging to check the value of imageData and image. Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure.

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