Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

iphone - NSData & NSURL - url with space having problem

I have following code in my application.

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];

pathOfThumbNail has following path


http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg


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


But in iPhone, due to space in path, image isn't loaded in nsdata.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use: stringByAddingPercentEscapesUsingEncoding:

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

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

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 output:

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'  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...