问题描述
我的服务器上有一些用特殊字符(å,ä,ö)命名的图像.我不知道如何转换NSUrl来获取它们:
I have some images on my server, named with special characters (å,ä,ö). I can't figure out how to convert the NSUrl to get them:
NSString *urlString = [NSString stringWithFormat: @".../Images/%@Image.png", playerName];
NSURL *url = [NSURL URLWithString: [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"url:%@", url);
//Get the image and assign it to the player
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
上面示例中的日志(如果"playerName" =åäö):
The log in the above example (if "playerName" = åäö):
.../Images/%C3%A5%C3%A4%C3%B6Image.png
但我需要这样:
.../Images/åäöImage.png
我尝试了不同的(stringByAddingPercentEscapesUsingEncoding)和下面的代码,但未成功:
I tried different (stringByAddingPercentEscapesUsingEncoding) and the code below without success:
- (NSString *)URLEncodingOfString:(NSString *)s
{
return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes
(kCFAllocatorDefault, (__bridge CFStringRef)s, NULL, NULL,
kCFStringEncodingISOLatin1);
}
任何建议都非常感谢,预先感谢
Any advice is very appreciated,Thanks in advance
推荐答案
我认为这完全取决于Web服务器使用的编码方式.
I think it'll depend entirely on what encoding the web server is using.
由于从您的测试看来ISO-Latin-1似乎无法正常工作,因此您可以尝试将kCFStringEncodingISOLatin1
替换为kCFStringEncodingUTF8
.
Since ISO-Latin-1 doesn't seem, from your test, to work, you might try replacing kCFStringEncodingISOLatin1
with kCFStringEncodingUTF8
.
这篇关于带有特殊字符的NSURL(åäö)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!