How Can I get Mime Type in IOS which is not based on extension

前端 未结 1 1843
生来不讨喜
生来不讨喜 2020-12-13 22:00

we know that we can get the file\'s MIME TYPE from the file\'s extension,but it\'s not exactly.for example we changed the file\'s extension and we will get the wrong mime ty

相关标签:
1条回答
  • 2020-12-13 22:36

    Would you tell me how I get the right mime type exactly when I get a path of a file.

    iOS uses the concept of Uniform Type Identifiers (UTI) to handle document types.

    NSString *path; // contains the file path
    
    // Get the UTI from the file's extension:
    
    CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
    CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
    CFRelease(pathExtension);
    
    // The UTI can be converted to a mime type:
    
    NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
    if (type != NULL)
        CFRelease(type);
    

    You should consider using UTIs for your purpose directly instead of converting them to the less powerful mime type.

    0 讨论(0)
提交回复
热议问题