从IPad中的UIActionSheet显示uiimagepickercontrollersourcetypephotolibrary的UIImagePickerViewController

时间:2012-04-02 07:08:20

标签: iphone ipad uiimagepickercontroller uipopovercontroller uiactionsheet

我有UIActionSheet,可以选择从相机或照片库中选择图像。为此,我采用了 imagePickerViewController 。对于相机,它的工作非常好。但不是照片库。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) 
    {

        imgController = [[UIImagePickerController alloc] init];
        imgController.allowsEditing = YES;
        imgController.sourceType =  UIImagePickerControllerSourceTypeCamera;   
        imgController.delegate=self;
        [self presentModalViewController:imgController animated:YES];

    } 
    else if (buttonIndex == 1) 
    {

        if ([self.popoverController isPopoverVisible]) {
            [self.popoverController dismissPopoverAnimated:YES];
            [popoverController release];
        } else {
            if ([UIImagePickerController isSourceTypeAvailable:
                 UIImagePickerControllerSourceTypeSavedPhotosAlbum])
            {
                UIImagePickerController *imagePicker =
                [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.sourceType =
                UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.allowsEditing = NO;

                self.popoverController = [[UIPopoverController alloc]
                                          initWithContentViewController:imagePicker];

                self.popoverController.delegate = self;

                [self.popoverController setPopoverContentSize:CGSizeMake(500, 500)];

                [self.popoverController presentPopoverFromRect:self.view.frame 
                                                          inView:self.view
                                        permittedArrowDirections:UIPopoverArrowDirectionAny 
                                                        animated:YES];


                [imagePicker release];
            }
        }


    } 

}  

在触摸照片库选项时,会在视图顶部创建一个小的 popupViewController ,但它非常小。为什么 popupViewController 太小了?是否有其他方法可以显示UIImagePickerViewController的{​​{1}}?

4 个答案:

答案 0 :(得分:3)

试试这个例子

   CGRect popoverRect = [self.view convertRect:[self.view frame] 
                                       fromView:[self.view superview]];

    popoverRect.size.width = MIN(popoverRect.size.width, 80) ; 
    popoverRect.origin.x  = popoverRect.origin.x+150; 

    [self.popoverController 
     presentPopoverFromRect:popoverRect 
     inView:self.view 
     permittedArrowDirections:UIPopoverArrowDirectionLeft
     animated:YES];

答案 1 :(得分:1)

我认为你传入的矩形应该更小,而不是整个视图的大小:

CGRect popFrom = CGRectMake (CGPointMake(50,50),10,10);
[self.popoverController presentPopoverFromRect:popFrom 
                                                          inView:self.view
                                        permittedArrowDirections:UIPopoverArrowDirectionAny 
                                                        animated:YES];

答案 2 :(得分:1)

尝试不设置弹出窗口中显示的PopoverContentSize UIImagePickerViewController - 我认为它会自动处理!此外,CGRect中的presentPopoverFromRect:似乎不正确,因为您正在传递view的框架,该框架会转换为大致指向屏幕中心的弹框。您可能希望将其设置为显示UIActionSheet的按钮的框架!

答案 3 :(得分:-1)

试试这个:

 imagePickerController = [[UIImagePickerController alloc] init];
 imagePickerController.delegate = self;
 imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 [self presentModalViewController:imagePickerController animated:YES];