【问题标题】:get MIME type from Assets Library URL iOS从资产库 URL iOS 获取 MIME 类型
【发布时间】:2015-08-02 23:25:51
【问题描述】:

我正在尝试上传图片,但在发布请求中我需要传递“Content-Type”,即我从 iOS 照片库中选择的图片的 mime 类型,但是当 我试图获取mime 类型将为零

这里是过程:-

注意: imgProfileURL 的类型为NSString

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        imgProfileURL=[NSString stringWithFormat:@"%@",[info objectForKey:@"UIImagePickerControllerReferenceURL"]];

    }

imgProfileURL 现在包含:-

assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG

现在,我正在调用下面的函数来获取 MIME 类型并传递 imgProfileURL 的值

- (NSString *)mimeTypeForPath:(NSString *)path
{
    // get a mime type for an extension using MobileCoreServices.framework



    CFStringRef extension = (__bridge CFStringRef)[path pathExtension];
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);
    assert(UTI != NULL);

    NSString *mimetype = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType));  //it is NULL
    assert(mimetype != NULL); // Crashing here

    CFRelease(UTI);

    return mimetype;
}

mimetype 此处为 NULL。

我已经解决了这个问题,但没有帮助我:- LINK

【问题讨论】:

    标签: ios objective-c iphone mime-types alassetslibrary


    【解决方案1】:

    这是我为获取图像名称、类型和大小所做的:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *imgSelected = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSURL *fileURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
            ALAssetRepresentation *representation = [myasset defaultRepresentation];
            NSString *fileName = [representation filename];
            NSString *fileSize = [NSString stringWithFormat:@"%lld",[representation size]];
            NSString *MIMEType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass
            ((__bridge CFStringRef)[representation UTI], kUTTagClassMIMEType);
    
            NSLog(@"file name : %@",fileName);
            NSLog(@"file size : %@",fileSize);
            NSLog(@"file type : %@",MIMEType);
        };
    
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init] ;
        [assetslibrary assetForURL:fileURL resultBlock:resultblock failureBlock:nil];
    }
    

    【讨论】:

    • 嗨。你能用照片框架给出等价物吗
    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2019-12-04
    • 2017-02-13
    • 2017-12-07
    相关资源
    最近更新 更多