iOS 从 URL 加载图像

发布于 2024-08-02 13:43:04 字数 719 浏览 5 评论 0原文

当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-08-09 13:43:04

valueForKey: 返回的 NSString 是如何形成的?是类似“image.png”的东西,还是“/image.png”?如果是前者,您需要使用 stringByAppendingPathComponent: 而不是 stringByAppendingString:

How is the NSString returned by valueForKey: formed? Is it something like "image.png", or is it "/image.png"? If it's the former, you'll want to use stringByAppendingPathComponent: instead of stringByAppendingString:.

归途 2024-08-09 13:43:04

您需要使用 UIWebView 并在其中加载 html
至少这是一个很好的解决方法。

检查此处的代码 http://blog.timeister.com /2009/07/18/iphone-show-online-image/

- (void)loadImage:(NSString*)url frame:(CGRect)frame {
NSString* textHTML = @"
<html><head>
<style type=\"text/css\">
body {
background-color: transparent;
color: white;
}
</style>
</head><body style=\"margin:0\">
<img  src=\"%@\"  width=\"%0.0f\" height=\"%0.0f\"></img>
</body></html>";

NSString* html = [NSString stringWithFormat:textHTML, url, frame.size.width, frame.size.height];

if(webView == nil) {
webView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:webView];
}

[webView loadHTMLString:html baseURL:nil];
}

阿德里安

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/

- (void)loadImage:(NSString*)url frame:(CGRect)frame {
NSString* textHTML = @"
<html><head>
<style type=\"text/css\">
body {
background-color: transparent;
color: white;
}
</style>
</head><body style=\"margin:0\">
<img  src=\"%@\"  width=\"%0.0f\" height=\"%0.0f\"></img>
</body></html>";

NSString* html = [NSString stringWithFormat:textHTML, url, frame.size.width, frame.size.height];

if(webView == nil) {
webView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:webView];
}

[webView loadHTMLString:html baseURL:nil];
}

Adrian

弥繁 2024-08-09 13:43:04

我认为问题与正确的编码有关。

尝试使用以下函数正确编码参数:

encodedparams = [params stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

所以在你的情况下:

NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ;
specificPath = [specificPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

我认为这会解决你的问题。

I think problem is related to proper encoding.

try encoding the parameters properly by using following function :

encodedparams = [params stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

So in your case :

NSString * specificPath = (NSString *)[item valueForKey:@"pictureLocation"] ;
specificPath = [specificPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

I think this will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文