简体   繁体   English

在uiimageview中显示来自URL的图像

[英]Show image from url in uiimageview

I try to show a image in my iphone app but it doesn't show. 我尝试在iPhone应用程序中显示图像,但不显示。 I connect in IB and I check to show a local image, it workd good. 我在IB中连接,然后检查以显示本地图像,效果很好。 I also check the url from image and it is ok. 我也检查图像的URL,没关系。 I use the next code: 我使用下一个代码:

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 Getts

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"]];

It can be very helpful to NSLog() yourself some notes about what's really in your variables. 对于NSLog()自己来说,有关变量中真正内容的一些注释可能非常有帮助。 If you really do have everything hooked up right in IB, it could be that aVideo.urlThumbnail isn't getting set. 如果您确实确实将所有内容都连接到IB中,则可能是未设置aVideo.urlThumbnail I'd like to see that value in the console right before creating your NSURL with it. 我想在使用它创建NSURL之前在控制台中看到该值。

First off, since NSData dataWithContentsOfURL: accesses the network, you should consider doing that in the background. 首先,由于NSData dataWithContentsOfURL:访问网络,因此您应该考虑在后台进行此操作。 Otherwise, your UI may become unresponsive. 否则,您的UI可能会无响应。

You should consider adding some additional logging to check the value of imageData and image . 您应该考虑添加一些其他日志记录以检查imageDataimage的值。 Also, you can call NSData dataWithContentsOfURL:options:error: to see if that's the source of the failure. 此外,您可以调用NSData dataWithContentsOfURL:options:error:来查看是否是失败的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM