NSString到NSURL转换删除路径的某些部分

时间:2016-12-30 05:55:51

标签: ios objective-c nsurl

我附加了我在控制台中记录的网址和路径的图像。当我将我的String转换为URL时,为什么会删除它的某些部分。

在网址中添加

“...”代替某条路径。

任何人都可以建议任何方法,它会在URL中为我提供完全相同的字符串。

NSString *path=[[NSBundle mainBundle] pathForResource:@"smiley" ofType:@"gif"];
NSURL *url = [[NSURL alloc] initWithString:path];

enter image description here

2 个答案:

答案 0 :(得分:2)

网址始终使用方案(http://ftp://file://等),从文件系统路径创建一个URL,以单斜杠开头,您必须使用API fileURLWithPath添加file://方案。

然而,最简单的解决方案是使用NSBundle的URL相关API:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"smiley" withExtension:@"gif"];

答案 1 :(得分:0)

试试这段代码,

NSString *path = [[NSBundle mainBundle] pathForResource:@"smiley" ofType:@"gif"];
NSURL *url = [NSURL fileURLWithPath:path];