On iPad, UIImagePickerController must be presented via UIPopoverController

来源:互联网 发布:手机休眠关闭网络 编辑:程序博客网 时间:2024/04/30 12:21

UIImagePickerController *m_imagePicker = [[UIImagePickerControlleralloc] init];
    if([UIImagePickerController isSourceTypeAvailable:
       UIImagePickerControllerSourceTypePhotoLibrary]){
      m_imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
      m_imagePicker.delegate = self;
      //      [m_imagePicker.navigationBar.subviews];
      [m_imagePicker setAllowsEditing:NO];
      //m_imagePicker.allowsImageEditing = NO;
       [selfpresentModalViewController:m_imagePicker animated:YES];
      [m_imagePicker release];
    }else{
       UIAlertView*alert = [[UIAlertView alloc]initWithTitle:nil message:@"Erroraccessing photo library!" delegate:nil cancelButtonTitle:@"Close"otherButtonTitles:nil];
       [alertshow];
       [alertrelease];
    }

这对iPhone的操作是没有问题的。但是当我们在iPad环境中却有问题了,当我们运行时会报如下错误:

Terminating app due to uncaught exception'NSInvalidArgumentException', reason: 'On iPad,UIImagePickerController must be presented viaUIPopoverController'

所以我们必须通过UIPopoverController来实现才行。具体实现如下:

UIImagePickerController *m_imagePicker = [[UIImagePickerControlleralloc] init];
    if([UIImagePickerController isSourceTypeAvailable:
       UIImagePickerControllerSourceTypePhotoLibrary]){
      m_imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
      m_imagePicker.delegate = self;
      [m_imagePicker setAllowsEditing:NO];
      UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:m_imagePicker];
      self.popoverController = popover;
      //popoverController.delegate = self;
       
      [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300,300) inView:self.viewpermittedArrowDirections:UIPopoverArrowDirectionAnyanimated:YES];
       
       //[selfpresentModalViewController:m_imagePicker animated:YES];
       [popoverrelease];
      [m_imagePicker release];
    }else{
       UIAlertView*alert = [[UIAlertView alloc]initWithTitle:nil message:@"Erroraccessing photo library!" delegate:nil cancelButtonTitle:@"Close"otherButtonTitles:nil];
       [alertshow];
       [alertrelease];
    }
这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。


原创粉丝点击