【问题标题】:How to get MIME type for image or video in iOS 8 using PHAsset?如何使用 PHAsset 在 iOS 8 中获取图像或视频的 MIME 类型?
【发布时间】:2015-04-27 18:39:04
【问题描述】:

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

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

寻求任何指导。

【问题讨论】:

  • 你找到解决方案了吗?

标签: ios ios8 mime-types phasset


【解决方案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】:

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

    @implementation PHAsset (UTI)
    
    - (NSString *)utiMimeType
    {
        NSString *mimeType = @"image/jpeg";
    
        NSString *uType = [PHAssetResource assetResourcesForAsset:self].firstObject.uniformTypeIdentifier;
        if (uType) {
            NSString *tagMimeType = (__bridge NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uType, kUTTagClassMIMEType);
            if (tagMimeType)
                mimeType = tagMimeType;
        }
    
        return mimeType;
    }
    
    @end
    

    提出几点建议/改进方法:

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

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

    【讨论】:

    • jpeg 是 iOS 上的默认图片格式吗?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 对不起,我忘了提及,但我使用相同的图像,我不知道如何获取视频的 mime 类型。
      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多