如何使用PHAsset在iOS 8中获取图像或video的MIME类型?

我在我的应用程序中使用PHAsset,我需要将图像和video上传到api,因为我需要mime类型的图像和video。 在iOS的早期版本中,我使用以下代码,但在iOS 8中我不知道如何获取mimetype我试过查看苹果PHAsset编程指南但无法找到它。

ALAssetRepresentation *representation = [asset defaultRepresentation]; NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass ((__bridge CFStringRef)[representation UTI], kUTTagClassMIMEType); 

寻找任何指导。

PHContentEditingInput具有属性uniformTypeIdentifier 。 您可以在文档中找到更多信息 。

 @import MobileCoreServices.UTType; ... PHAsset *asset = ... PHContentEditingInputRequestOptions *options = ... [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) { NSString *MIME = (__bridge NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)contentEditingInput.uniformTypeIdentifier, kUTTagClassMIMEType); }]; 

使用PHImageManager的requestImageDataForAsset方法。 在它的resultBlock中,该方法返回资产的UTI-Type。

您还可以从PHContentEditingInput类中找到uniformTypeIdentifier。 为了这; 使用PHAsset中的requestContentEditingInput函数

不要忘记导入MobileCoreServices

示例Swift 3.1代码:

 let options = PHContentEditingInputRequestOptions() options.isNetworkAccessAllowed = true //for icloud backup assets let asset : PHAsset = ..... //sampleAsset asset.requestContentEditingInput(with: options) { (contentEditingInput, info) in if let uniformTypeIdentifier = contentEditingInput?.uniformTypeIdentifier { //check type here if uniformTypeIdentifier == (kUTTypeGIF as String) { debugPrint("This asset is a GIF👍") } } }