NSString to NSurl

余生颓废 提交于 2019-11-30 00:09:52
cxa

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]];  

For IOS ≥ 9.0 use

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

All is ok, if you want to get the URL to print in NSLog use this:

NSLog(@"url> %@ ", [url absoluteString]);

item.image does not only contain the URL, but it starts with a newline and spaces. Remove those first and you should be fine.

In Swift:

NSURL(fileURLWithPath: item.image!)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!