NSData& NSURL - 空间有问题的网址 [英] NSData & NSURL - url with space having problem
问题描述
我的应用程序中有以下代码。
I have following code in my application.
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
pathOfThumbNail有以下路径
pathOfThumbNail has following path
http://70.84.58.40/projects/igolf/TipThumb/ GOLF 58B.jpg
当我在safari浏览器中打开上述路径时 - 路径自动更改&图像已成功显示。
When I open above path in safari browser - path is changed automatically & image is successfully displayed.
http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg
但在iPhone中,由于路径中的空间,图像未加载到nsdata中。
But in iPhone, due to space in path, image isn't loaded in nsdata.
推荐答案
使用:stringByAddingPercentEscapesUsingEncoding:
Use: stringByAddingPercentEscapesUsingEncoding:
使用给定的编码返回接收器的表示,以确定将接收器转换为合法URL字符串所需的转义百分比。
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
接收器的表示,使用编码来确定将接收器转换为合法URL字符串所需的转义百分比。如果编码不能编码特定字符,则返回nil
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character
@rule每个请求添加
Added per request by @rule
NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(@"urlText: '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url: '%@'", url);
NSLog输出:
urlText: '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg'
urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
url: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
这篇关于NSData& NSURL - 空间有问题的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!