How to get MIME type for image or video in iOS 8 u

2020-06-28 05:00发布

问题:

I am using PHAsset in my application where I need to upload image and video to api, for that I need mime type of image and video. In previous version of iOS I was using following code but in iOS 8 I don't know how to get mimetype I have tried looking into apple PHAsset programming guide but unable to find it.

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

Looking for any guidance.

回答1:

PHContentEditingInput has property uniformTypeIdentifier. You can find more in 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);
}];


回答2:

Also you can find uniformTypeIdentifier from PHContentEditingInput class. For this; use requestContentEditingInput function from PHAsset

Don't forget to import MobileCoreServices

Sample Swift 3.1 code:

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