xcode 从 iOS 8 中的 PHAsset 获取文件扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25998153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:40:04  来源:igfitidea点击:

Get File Extension from a PHAsset in iOS 8?

iosobjective-cxcodeios8

提问by Rob Caraway

Is there any way to distinguish a PHAsset's file extension? My app needs to know if the photo is a .GIF or not. I'm not seeing any way to do this.

有没有办法区分PHAsset的文件扩展名?我的应用程序需要知道照片是否为 .GIF。我没有看到任何方法可以做到这一点。

采纳答案by rickster

File names are an implementation detail, and may or may not tell you what the content of a file really is.

文件名是一个实现细节,可能会也可能不会告诉您文件的内容到底是什么。

When you call requestImageDataForAsset, the dataUTIparameter that Photos passes to your completion handler tells you the Uniform Type Identifier for the image data.

当您调用 时requestImageDataForAssetdataUTIPhotos 传递给完成处理程序的参数会告诉您图像数据的统一类型标识符。

回答by Bob Easterday

A better approach would be to use uniformTypeIdentifier. Files can be named anything and may or may not contain a file extension.

更好的方法是使用uniformTypeIdentifier。文件可以命名为任何名称,可能包含也可能不包含文件扩展名。

[asset valueForKey:@"uniformTypeIdentifier"];

回答by Adarsh G J

Using valueForKey: you can get the PHAsset file name from that you extract file extension using "pathExtension" For Example: PHAsset *selectedAsset = ...

使用 valueForKey:您可以从使用“pathExtension”提取文件扩展名的文件中获取 PHAsset 文件名,例如:PHAsset *selectedAsset = ...

NSString *fileName =[selectedAsset valueForKey:@"filename"];
NSString * fileExtension = [fileName pathExtension];

回答by funct7

PHAssetResource.uniformTypeIdentifierseems to do the job. PHAssets can be fetched by using the class function PHAssetResource. assetResources(for:).

PHAssetResource.uniformTypeIdentifier似乎做的工作。 PHAssets 可以通过使用类函数来获取PHAssetResource. assetResources(for:)

An image does NOTmean only there will be only one item in the list, so check out the documentation.

图像确实不是意味着只有在那里将只有一个列表中的项目,所以检查出的文档。

回答by Anand Gautam

In Swift, Use:

在 Swift 中,使用:

URL(fileURLWithPath: (asset.value(forKey: "filename") as! String)).pathExtension