问题描述
我使用此代码从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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!