我试图通过其路径获取文件的大小。这些文件是我应用中保存的视频。我使用下面的代码返回错误,说没有文件或目录。
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"];
}
引用根目录的路径称为绝对路径。引用当前目录的路径称为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已经回答的答案