我正在尝试上传图片,但是在帖子请求中我需要传递“内容类型”,即我从iOS照片库中选择的图像的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
此处为空。
我已经完成了这个问题,但没有帮助我: - LINK
答案 0 :(得分: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];
}