Ios 数据总是空的。。。不知道为什么
test.mp4在我的包中,但这始终是输出Ios 数据总是空的。。。不知道为什么,ios,nsdata,nsurl,mp4,Ios,Nsdata,Nsurl,Mp4,test.mp4在我的包中,但这始终是输出 NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]; NSLog(@"Path: %@", path); NSURL *url = [NSURL URLWithString:path]; NSLog(@"URL: %@", url); NSData *data = [NSData dataWithContentsOfURL:url]; N
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSLog(@"Path: %@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"URL: %@", url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"Data exists? %@", data ? @"yes" : @"no");
我不知道为什么,但数据从不存在。您正在检索文件路径,然后将其用作URL。您要检索URL并使用URL:
2013-03-13 11:50:29.444 videoAppPrototype[28625:907] Path: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.446 videoAppPrototype[28625:907] URL: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.448 videoAppPrototype[28625:907] Data exists? no
或者要检索路径并使用路径:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"];
NSData *data = [NSData dataWithContentsOfURL:url];
正如bgoers所指出的,如果您有一个路径并且需要一个文件URL,那么您可以使用fileURLWithPath
,尽管我更愿意使用上述两种技术之一。无论如何,要使用fileURLWithPath
,如下所示:
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSData *data = [NSData dataWithContentsOfFile:path];
您正在检索文件路径,然后将其用作URL。您要检索URL并使用URL:
2013-03-13 11:50:29.444 videoAppPrototype[28625:907] Path: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.446 videoAppPrototype[28625:907] URL: /var/mobile/Applications/D66DD820-4DE1-4E45-9495-9BACF130E368/videoAppPrototype.app/test.mp4
2013-03-13 11:50:29.448 videoAppPrototype[28625:907] Data exists? no
或者要检索路径并使用路径:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"];
NSData *data = [NSData dataWithContentsOfURL:url];
正如bgoers所指出的,如果您有一个路径并且需要一个文件URL,那么您可以使用fileURLWithPath
,尽管我更愿意使用上述两种技术之一。无论如何,要使用fileURLWithPath
,如下所示:
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSData *data = [NSData dataWithContentsOfFile:path];
您确定应用程序捆绑包中存在
test.mp4
吗?是否存在的可能重复?是否确定应用程序捆绑包中存在test.mp4
的可能重复?是否可以解释为什么会失败?URL看起来是正确的。长话短说,你不能在iOS中很好地混合URL和文件路径:)我相信他也可以使用[NSURL fileURLWithPath:path]
来访问URL路径。但最好使用上述两种方法之一。@如果URLForResource
中的URL以file://localhost/
后跟路径。当您只是检索一条路径时,您将丢失该路径。pathForResource
的结果不是有效的URL字符串。@Rob Yeah,我知道;这就是为什么这个问题是个骗局。你能解释一下为什么它会失败吗?URL看起来是正确的。长话短说,你不能在iOS中很好地混合URL和文件路径:)我相信他也可以使用[NSURL fileURLWithPath:path]
来访问URL路径。但最好使用上述两种方法之一。@如果URLForResource
中的URL以file://localhost/
后跟路径。当您只是检索一条路径时,您将丢失该路径。pathForResource
的结果不是有效的URL字符串。@Rob Yeah,我知道;这就是为什么这个问题是个骗局。