Ios7 UIImagePickerController图像类型

Ios7 UIImagePickerController图像类型,ios7,uiimage,uiimagepickercontroller,Ios7,Uiimage,Uiimagepickercontroller,我的应用程序允许用户从设备摄像机卷中选择图像。我想验证所选图像的格式是否为PNG或JPG图像 是否可以在-(void)imagePickerController:(UIImagePickerController*)PickerDidFinishPickingMediaWithInfo:(NSDictionary*)info委托方法中执行此操作?是的,您可以在委托回调中执行此操作。您可能已经注意到,UIImagePickerControllerMediaTypeinfo字典键将返回一个“publi

我的应用程序允许用户从设备摄像机卷中选择图像。我想验证所选图像的格式是否为PNG或JPG图像


是否可以在
-(void)imagePickerController:(UIImagePickerController*)PickerDidFinishPickingMediaWithInfo:(NSDictionary*)info
委托方法中执行此操作?

是的,您可以在委托回调中执行此操作。您可能已经注意到,
UIImagePickerControllerMediaType
info字典键将返回一个“public.image”字符串作为UTI,这对于您的目的来说是不够的。但是,这可以通过使用与信息字典中的
UIImagePickerControllerReferenceURL
键关联的url来实现。例如,实现可能类似于下面的方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    NSURL *assetURL = info[UIImagePickerControllerReferenceURL];

    NSString *extension = [assetURL pathExtension];
    CFStringRef imageUTI = (UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(__bridge CFStringRef)extension , NULL));

    if (UTTypeConformsTo(imageUTI, kUTTypeJPEG))
    {
        // Handle JPG
    }
    else if (UTTypeConformsTo(imageUTI, kUTTypePNG))
    {
        // Handle PNG
    }
    else
    {
        NSLog(@"Unhandled Image UTI: %@", imageUTI);
    }

    CFRelease(imageUTI);

    [self.imageView setImage:image];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}
您还需要链接MobileCoreServices.framework并添加一个
#import