UIImagePickerController in UIPopOverController is

2019-08-19 16:57发布

问题:

in my iPad app i am opening Photo Library in a popover controller. It was working fine in iOS 4 but now its not opening up in iOS 5. i am using the following code for opening Photo Library,

UIImagePickerController* picker = [[UIImagePickerController alloc] init]; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    popOver = [[UIPopoverController alloc] initWithContentViewController:picker]; 
    popOver.delegate = self;

    int w = 320;

    CGRect pickerFrame = CGRectMake(0, 0, w, bImportPicker.frame.origin.y);
    [popOver setPopoverContentSize:pickerFrame.size animated:NO];   
    [popOver presentPopoverFromRect:pickerFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    [picker release];

回答1:

In my code, I had a UIImageView, whenever I tapped on that, the PickerView with Images from iPhone Library gets opened in PopOverController.

  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

   [myImageView addGestureRecognizer:singleTap];   // added action for SingleTap




  - (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {  
// single tap does nothing for now  

    if ([UIImagePickerController isSourceTypeAvailable: 
                UIImagePickerControllerSourceTypePhotoLibrary]) 
     { 
          UIImagePickerController  *imagePickerController = 
                     [[UIImagePickerController alloc] init];
          imagePickerController.delegate = self;
          imagePickerController.sourceType =
                     UIImagePickerControllerSourceTypePhotoLibrary;

          UIPopoverController  *popVC = [[UIPopoverController alloc] 
                                 initWithContentViewController: imagePickerController];
          popVC.delegate = self; 
         [popVC setPopoverContentSize:CGSizeMake(500, 500)];


         UIView *tempView = gestureRecognizer.view;        
         CGPoint point = CGPointMake(tempView.frame.size.width/2,
                               tempView.frame.size.height/2);
         CGSize size = CGSizeMake(100, 100);
         [popVC presentPopoverFromRect:
                           CGRectMake(point.x, point.y, size.width, size.height) 
                           inView:self.view 
                           permittedArrowDirections:UIPopoverArrowDirectionAny 
                           animated:YES];

       }
     else 
     {
         UIAlertView *alert = [[UIAlertView alloc] 
                             initWithTitle:@"Error accessing photo library"
                             message:@"Device does not support a photo library" 
                             delegate:nil cancelButtonTitle:@"Cancel" 
                             otherButtonTitles:nil]; 
         [alert show]; 
         [alert release];
      }

}

Happy coding.



回答2:

When my poor customers moved to iOS 5, their UIPopoverController was being drawn off the edge of the screen. This is because iOS 5 differs from iOS 4 in its interpretation of presentPopoverFromRect's first parameter. When I made sure the supplied rect leaves enough room for your UIImagePickerController between the rect and the edge of the display, the problem was resolved. Using the entire display for the rect will produce this misbehavior, which resembles what you've described.