无法让 NSSTRING 转换为 NSURL,item.image 保存了我通过 xml 获取的图像的 url
having trouble getting NSSTRING to convert to NSURL, item.image, holds the url for an image that im getting through 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)
如果我尝试:
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 -- /
从 NSString
制作 URL
时,别忘了先编码,试试这个:
When making URL
from NSString
, don't forget to encode it first, so try this:
NSString *urlString = [NSString stringWithFormat:@"%@", item.image];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
对于 IOS ≥ 9.0 使用
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
这篇关于NSString 到 NSurl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!