如何使用 UIDocumentPickerViewController 获取原始文件名?

How can i get the original file name using UIDocumentPickerViewController?

我需要知道我要导入的文件类型。 我可以从文件名中找出类型

对于 PHAsset,您可以使用以下方法:

Attachment *attachment = [[Attachment alloc] init];
                         attachment.assetIdentifierInternal = asset.localIdentifier;
                         attachment.descriptionInfo = [[NSProcessInfo processInfo] globallyUniqueString];
                         attachment.isPhoto = YES;
                         attachment.isDocument = NO;

 NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
 NSString *fileName = ((PHAssetResource *)resources[0]).originalFilename;
  attachment.format = [fileName pathExtension];

DocumentPicker 有类似的东西吗?

descriptInfo 显示没有问题。姓名 - 无。

Setter 姓名:

- (void)setName:(NSString *)name {

_name = name;

self.format = [name pathExtension];

CFStringRef fileExtension = (__bridge CFStringRef) self.format;
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

self.isPhoto = UTTypeConformsTo(fileUTI, kUTTypeImage);
self.isDocument = UTTypeConformsTo(fileUTI, kUTTypeData); }

经过一番搜索,我自己找到了解决方案。 URL 很容易跟踪名称和格式。像这样

    attachment.fileUrlInternal = url;
    attachment.format = [attachment.fileUrlInternal pathExtension];
    attachment.name = url.lastPathComponent;