NSString到NSurl

use*_*860 22 iphone objective-c iphone-sdk-3.0 ios4

无法让NSSTRING转换为NSURL,item.image,保存我通过xml的图像的网址

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:urlString];

NSLog(@"string> %@ ", urlString);
NSLog(@"url> %@ ", url);

2011-06-23 11:18:49.610 Test[10126:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg       
2011-06-23 11:18:49.611 Test[10126:207] url> (null) 
Run Code Online (Sandbox Code Playgroud)

如果我尝试:

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL fileURLWithPath :urlString];

2011-06-23 11:22:08.063 Test[10199:207] string> http://www.harlemfur.com/images/Dog_Olive.jpg

2011-06-23 11:22:08.064 Test[10199:207] url> %0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://www.harlemfur.com/images/Dog_Olive.jpg%0A%20%20%20%20%20%20%20%20%20%20%20%20 -- / 
Run Code Online (Sandbox Code Playgroud)

cxa*_*cxa 78

制作URLNSString,不要忘记先编码,所以试试这个:

NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
Run Code Online (Sandbox Code Playgroud)

对于IOS≥9.0使用

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
Run Code Online (Sandbox Code Playgroud)