UIPopverController,UIImagePickerController,图片选取。

/*使用UIImagePickerController和UIPopverController, 先在头文件中设置要遵循的协议:<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIPopoverControllerDelegate>*/

@implementation PickerVC

{

    /*UIPopoverController must use the global variable*/

    UIPopoverController *_popController;

     UIBarButtonItem *_popbbi;

    UIImageView *_imageView;

 } 

#pragma mark - UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    /*

     1.implicit conversion of Objective-C pointer type 'NSDictionary *'to C pointer type 'CFTypeRef'(asa'const void'*)required a bridged cast

     2.Use __bridge to convert directly(no change in ownership)

     3.void CFShow (CFTypeRef obj);Prints a description of a Core Foundation object to stderr.

     */

    CFShow((__bridge CFTypeRef)(info));

     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 300, 300)];

    _imageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    [self.view addSubview:_imageView]; 

    //如果是在iPad中使用的UIPopverController,则下面这个方法没有被调用。

    [self dismissViewControllerAnimated:YES completion:^{

        printf("UIImagePickerControllerEditedImage:edited chose! ");

    }];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:^{

        printf("dismissViewController! ");

    }];

}

-(void)pickImage:(id)sender{

    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    ipc.delegate = self;

    ipc.allowsEditing = YES;//这个设置是选取图片的关键。

    //进行硬件环境判断,如果是iPad,可以使用UIPopoverController,但是如果是在iPhone环境中则不能使用UIPopverController,否则会Crash。

    if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        _popController = [[UIPopoverController alloc] initWithContentViewController:ipc];

        _popController.delegate = self;

        [_popController presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        return;

    }

    //iPhone中一般使用下面这个方法就可以了。iPad也可以使用

    [self presentViewController:ipc animated:YES completion:^{

    }];

}

-(void)popbbiClick:(UIBarButtonItem *)popbbi{

    printf("click popbbi ");

    PickImageViewController *pvc = [[PickImageViewController alloc] init];

    [self.navigationController pushViewController:pvc animated:YES];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    self.navigationItem.rightBarButtonItem = kBarButton(@"Pick", @selector(pickImage:));

    self.title = @"Image Picker";

        _popbbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(popbbiClick:)];

    self.navigationItem.leftBarButtonItem = _popbbi;

}

 =======================下面再使用一次

@implementation PickImageViewController

{

    UIImagePickerController *_pvc;

    UIPopoverController *_popController;

    UIImageView *_imageView;

    UIImage *_image;

}

 -(void)initUIItems{

    _pvc = [[UIImagePickerController alloc] init];

    _pvc.delegate = self;

    _pvc.allowsEditing = YES;//要使用选中的Medio内容需要设置这个为YES。

    _pvc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];

    _imageView.backgroundColor = [UIColor yellowColor];

    _imageView.userInteractionEnabled = YES;

    [self.view addSubview:_imageView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouched:)];

    tap.numberOfTapsRequired = 2;

    [_imageView addGestureRecognizer:tap];

}

-(void)tapTouched:(UIGestureRecognizer *)tap{

     if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        UIPopoverController *popvc = [[UIPopoverController alloc] initWithContentViewController:_pvc];

        popvc.delegate = self;

        /*这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。

         Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception. */

        _popController = popvc;

        [_popController presentPopoverFromRect:CGRectMake(100, 100, 400,400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        return;

    }

    /*在iPhonez中可以直接使用下面的方法,但是在iPad中需要使用到上面的UIPopoverController,但是记住还是需要ImagePicker的代理方法,否则选不到图片,其实不同点就在上面这一点上。*/

    [self presentViewController:_pvc animated:YES completion:^{

        printf("PresentViewController:  open ImagePicker menu! ");

    }];

}

/*下面的代理方法依然不变!*/

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    _image = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    _imageView.image = _image;

    [self dismissViewControllerAnimated:YES completion:^{

        printf("Picker choose the oneImage and dismissViewController ");

    }];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [self dismissViewControllerAnimated:YES completion:^{

        printf("ImagePicker menu DismissViewController ");

    }];

}

=======

对于iPad来说,可以选择UIPopverController来使用UIImagePickerController,

因为iPad的大屏关系,这样使用其实有更好的用户体验,另外注意代理设置,可编辑属性设置,

    _pvc.allowsEditing = YES;//要使用选中的Image需要设置这个为YES。

    ipc.allowsEditing = YES;//这个设置是选取图片的关键。

 注意UIPopverController只能再iPad环境下使用。

     if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {}else Crash~

原文地址:https://www.cnblogs.com/longtaozi/p/3836641.html