从资产库获取 MIME 类型 URL iOS
get MIME type from Assets Library URL iOS
我正在尝试上传图片,但在 post 请求中我需要传递 "Content-Type" 即我从 iOS 照片库中选择的图片的 mime 类型,但是当我试图获取 mime 类型时,它将变为 nil。
过程如下:-
注意: 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
这是我获取图像名称、类型和大小的方法:
- (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];
}
我正在尝试上传图片,但在 post 请求中我需要传递 "Content-Type" 即我从 iOS 照片库中选择的图片的 mime 类型,但是当我试图获取 mime 类型时,它将变为 nil。
过程如下:-
注意: 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
这是我获取图像名称、类型和大小的方法:
- (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];
}