NSURL isFileURL always returns NO

心已入冬 提交于 2020-01-01 04:20:27

问题


Can someone explain to me why the following code:

NSString* filePathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSLog(@"%i", [[NSURL URLWithString:filePathString] isFileURL]);
NSLog(@"%@", filePathString);

outputs:

0

"/var/mobile/Applications/28ADFC19-874C-4304-94B5-F6441CAE9FAD/Documents"

This means that this url isn't a file URL. Obviously, it is.


Background:

I'm trying to use an AVCaptureMovieFileOutput to write a movie recording to a file, but the file url I'm proving gives the error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - Cannot record to URL /var/mobile/Applications/28ADFC19-874C-4304-94B5-F6441CAE9FAD/Documents/media/CrKNjNhe9so2LRnD9iHK.mov because it is not a file URL.'

That looks like a file URL to me, just like the original example. What gives?


回答1:


If you want to create an NSURL from a file path, you must use NSURL fileURLWithPath:. The use of URLWithString assumes the string is a non-file URL.

An NSURL is a file URL only if it begins with file://.



来源:https://stackoverflow.com/questions/13557191/nsurl-isfileurl-always-returns-no

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