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

时间:2015-02-25 16:41:05

标签: ios ios8 mime-types phasset

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

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

寻找任何指导。

4 个答案:

答案 0 :(得分:1)

PHContentEditingInput拥有财产uniformTypeIdentifier。您可以在the documentation找到更多信息。

@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);
}];

答案 1 :(得分:1)

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

别忘了导入MobileCoreServices

示例Swift 3.1代码:

import MobileCoreServices


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")
        }

    }
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我最终获得了PHAsset的MIME类型,如下所示(iOS 9 +):

1  is divided by 1
2  is divided by 1
2  is divided by 2
3  is divided by 1
3  is divided by 3
4  is divided by 1
4  is divided by 2
4  is divided by 4
5  is divided by 1
5  is divided by 5
6  is divided by 1
6  is divided by 2
6  is divided by 3
6  is divided by 6
7  is divided by 1
7  is divided by 7
8  is divided by 1
8  is divided by 2
8  is divided by 4
8  is divided by 8
9  is divided by 1
9  is divided by 3
9  is divided by 9

将事物与呼唤相结合/加以改进:

  1. 如果无法导出mimeType,我选择默认为image / jpeg。
  2. 假设第一个资源是适当的资源。多资源资产(实时照片等)可能并非如此。
  3. 我尚未针对iCloud中需要下载的资产测试其运行方式/是否按预期工作。

如果有人对#2或#3有见识,将不胜感激。当我发现更多信息时,我会更新我的答案。