1

I am trying to upload image, but in the post request I need to pass "Content-Type" i.e the mime type of the image which I have picked from iOS Photo library, but when I am trying to get the mime type it is coming nil.

Here is the process :-

Note: imgProfileURL is of type NSString

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

    }

imgProfileURL now contains:-

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

Now, I am calling the below function to get MIME type and passing the value of 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 is coming NULL here.

I have gone through this question , but didn't help me:- LINK

Community
  • 1
  • 1
Vizllx
  • 9,135
  • 1
  • 41
  • 79

1 Answers1

2

This is What I did to get image name, type and size:

- (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];
}
Bista
  • 7,869
  • 3
  • 27
  • 55