+ [NSURL URLWithString:]返回nil [英] +[NSURL URLWithString:] returning nil

查看:94
本文介绍了+ [NSURL URLWithString:]返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码从URL加载UIImage:

  NSURL *imageURL = [NSURL URLWithString:@"http://testwebsite.com/image.png"];
  NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
  imageView.image = [UIImage imageWithData: imageData];

但是我被困在一个看起来像这样的URL上:
http://www.testwebsite.com/getFile?userID=123
这在浏览器中工作正常,但在上面的imageData变量中返回nil.

but I'm stuck at a URL that looks like this one :
http://www.testwebsite.com/getFile?userID=123
this works fine in the browser but returns nil in the imageData variable above.

如何从这样模糊的URL(即不显示文件名而是重定向到图像文件的URL中)加载图像?

how can I load the image from such an obscured URL (i.e. from a URL that does not show off the file name but rather redirects to the image file) ?

先谢谢您

推荐答案

出于某些奇怪的原因,我必须使用:
[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

for some weird reason I have to use :
[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

当我创建URL并解决了问题时.

when I create the URL and this solved the issue.

这篇关于+ [NSURL URLWithString:]返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆