【问题标题】:No such file or directory iOS没有这样的文件或目录 iOS
【发布时间】:2019-03-20 05:52:51
【问题描述】:

我正在尝试通过其路径获取文件的大小。这些文件是我的应用程序中保存的视频。我使用下面的代码返回错误,说没有文件或目录。

NSString *urlPath = [self.localPathArray objectAtIndex:indexPath.row];
NSError *err;
NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:urlPath error:&err];

这是下载文件后保存文件路径的方式

 - (void)URLSession:(NSURLSession *)session assetDownloadTask:
(AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {


        NSString *localPath = location.relativePath;
        NSLog(@"localPath: %@", localPath);

       [[NSUserDefaults standardUserDefaults]setValue:self.localPathArray forKey:@"AssetPath"];
    }

请参阅下面的错误屏幕截图和我的应用存储详细信息屏幕截图

【问题讨论】:

  • 你能打印你的urlPath吗
  • 是的,我可以打印文件路径,我可以使用avplayer中的文件路径播放视频
  • @Codecracker 这些路径是如何存储的?
  • 来自 didFinishDownloadingToURL 方法,我将其保存在 NSUserdefaults 中
  • @Codecracker 您可以在问题中添加该代码吗?

标签: ios objective-c nsfilemanager


【解决方案1】:

引用根目录的路径称为绝对。引用当前目录的路径称为relative

那就试试吧

 NSString *localPath = location.path;

而不是

  NSString *localPath = location.relativePath;

例如

  - (void)URLSession:(NSURLSession *)session assetDownloadTask:
(AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {


        NSString *localPath = location.path;
        NSLog(@"localPath: %@", localPath);
        [self.localPathArray addObject: localPath];
       [[NSUserDefaults standardUserDefaults]setValue:self.localPathArray forKey:@"AssetPath"];
    }

对于示例,请参阅已在 Stack overflow 中回答的此答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-24
    • 2016-03-14
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多