iOS 从 URL 加载图像
当我将 UIImage 从 URL 加载到 iPhone 上的 UIImageView 时,我遇到了一些问题。当我使用对象的属性生成 NSURL(使用 NSURL 的 URLWithString 方法的参数中的 URL)时,根本不会检索图像,但是如果我硬编码相同的 URL,则会按预期检索并显示图像。
[item valueForKey:@"pictureLocation"];下面的部分似乎是导致问题的原因,因为即使连接两个硬编码字符串,生成 NSURl 也没有问题。
NSString * imagePath = @"http://localhost:3000";
NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ;
//concatenate the strings to get a fully formed URL
NSString * finalPath = [imagePath stringByAppendingString:specificPath];
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:finalPath]]] retain];
本质上,当我 NSLog 最终路径并将该 URL 硬编码到程序中时,我得到了预期的结果。
为什么会出现这种情况?
I am experiencing some issues when I load a UIImage from a URL into a UIImageView on the iPhone. When I generate the NSURL (with the URL in the argument of NSURL's URLWithString method) using properties of an object, the image is not retrieved at all, however if I hardcode the same URL, the image is retrieved and displayed as expected.
The [item valueForKey:@"pictureLocation"]; part below seems to be what is causing the problem because even when two hardcoded strings are concatenated, the NSURl is generated with no issues.
NSString * imagePath = @"http://localhost:3000";
NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ;
//concatenate the strings to get a fully formed URL
NSString * finalPath = [imagePath stringByAppendingString:specificPath];
UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:finalPath]]] retain];
Essentially, when I NSLog the finalPath and instead hardcode that URL into the program, I get the expected result.
Why this might be the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
valueForKey:
返回的NSString
是如何形成的?是类似“image.png”的东西,还是“/image.png”?如果是前者,您需要使用stringByAppendingPathComponent:
而不是stringByAppendingString:
。How is the
NSString
returned byvalueForKey:
formed? Is it something like "image.png", or is it "/image.png"? If it's the former, you'll want to usestringByAppendingPathComponent:
instead ofstringByAppendingString:
.您需要使用 UIWebView 并在其中加载 html
。
至少这是一个很好的解决方法。
检查此处的代码 http://blog.timeister.com /2009/07/18/iphone-show-online-image/
阿德里安
You need to use the UIWebView and load a html
<img />
inside it.At least this is a nice workaround.
Check the code here http://blog.timeister.com/2009/07/18/iphone-show-online-image/
Adrian
我认为问题与正确的编码有关。
尝试使用以下函数正确编码参数:
所以在你的情况下:
我认为这会解决你的问题。
I think problem is related to proper encoding.
try encoding the parameters properly by using following function :
So in your case :
I think this will solve your problem.