简体   繁体   English

我如何从网址显示图像?

[英]how can i display image from url?

I have a string variable tmpImgURLStr which contains URL like www.abc.com/img.png . 我有一个字符串变量tmpImgURLStr ,其中包含www.abc.com/img.png URL。 i want to display that image in my imageView for that i have use some code but it's not working which is given below: 我想在我的imageView中显示该图像,因为我已经使用了一些代码,但它不起作用,如下所示:

NSLog(@"Img URL === %@",tmpImgURLStr);

NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",tmpImgURLStr]]]; 

UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];

As far as I can tell from your url - you have pdf, not an image. 据我所知,你的网址是pdf,而不是图片。 Usually WebView s are used for displaying this sort of data. 通常, WebView用于显示此类数据。

Update 更新

Your NSData initiation is kinda too long. 您的NSData启动有点太长了。 You can actually initiate a URL without supplying formatted string: 实际上,您可以在不提供格式化字符串的情

NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]];

Also I've noticed that your URL is without protocol. 另外我注意到你的URL没有协议。 You may want to try adding http:// or https:// to it and then see what happens. 您可能想尝试将http://https://到其中,然后查看会发生什么。 And just in case check if your logoImg is actually wired to the NSImageView in your NIB. 以防万一你的logoImg实际连接到NIB中的NSImageView

Try this 试试这个

NSURL *imageurl = [NSURL URLWithString:@"http://www.chakrainteractive.com/mob/ImageUpoad/pic2-2.png"];

NSData *imagedata = [[NSData alloc]initWithContentsOfURL:imageurl];

UIImage *image = [UIImage imageWithData: imagedata];

[logoImg setImage: image];

Try this code instead of yours .. May be it will work.. 尝试使用此代码代替您的代码..可能它会工作..

logoImg=[[IUImageView alloc]initWithFrame:CGRectMake(10,10,300,460)];
NSLog(@"Img URL === %@",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];

-Happy coding.... - 快乐编码....

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

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