如何显示网址中的图片? [英] how can i display image from url?
问题描述
我有一个字符串变量tmpImgURLStr
,其中包含类似www.abc.com/img.png
的URL.我想在我的imageView中显示该图像,因为我已经使用了一些代码,但是它不能正常工作,如下所示:
I have a string variable tmpImgURLStr
which contains URL like www.abc.com/img.png
. 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:
NSLog(@"Img URL === %@",tmpImgURLStr);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",tmpImgURLStr]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[logoImg setImage:myimage];
推荐答案
据我从您的网址可以得知-您有pdf,而不是图片.通常WebView
用于显示此类数据.
As far as I can tell from your url - you have pdf, not an image. Usually WebView
s are used for displaying this sort of data.
更新
您的NSData
初始化时间太长.实际上,您可以在不提供格式化字符串的情况下启动URL:
Your NSData
initiation is kinda too long. You can actually initiate a URL without supplying formatted string:
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:tmpImgURLStr]];
我也注意到您的网址没有协议.您可能想要尝试向其中添加http://
或https://
,然后查看会发生什么.并且以防万一您的logoImg
是否实际上已连接到NIB中的NSImageView
.
Also I've noticed that your URL is without protocol. You may want to try adding http://
or https://
to it and then see what happens. And just in case check if your logoImg
is actually wired to the NSImageView
in your NIB.
这篇关于如何显示网址中的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!