How to use UIImagePickerController in iPad?

帅比萌擦擦* 提交于 2019-11-27 08:19:49

UIImagePickerController must be presented with UIPopoverController on iPad.

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    self.popOver = popover;
} else {
    [self presentModalViewController:picker animated:YES];
}

EDIT: Add a strong property for the UIPopoverController:

@property (nonatomic, strong) UIPopoverController *popOver;

The popover should be dismissed in the delegate methods:

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

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 

Here i show you the SWIFT way:

import UIKit
class StoreItemViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var productImage: UIImageView!
    var popOver:UIPopoverController?

    @IBAction func buttonSelected(sender:UIButton)
    {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)
        {
            var imagePickerController = UIImagePickerController()
            imagePickerController.delegate = self
            imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
            imagePickerController.allowsEditing = false

            if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
            {
                self.popOver = UIPopoverController(contentViewController: imagePickerController)
                self.popOver?.presentPopoverFromRect(self.productImage.bounds, inView: self.productImage, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)      
            }
            else
            {
                self.presentViewController(imagePickerController, animated: true, completion: { imageP in

                })
            } 
        }
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
         //do anything with the image
        let selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage

        //closing the popup
        popOver?.dismissPopoverAnimated(true)

    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) 
    {
         println("cancel")

       //closing the popup
       popOver?.dismissPopoverAnimated(true)
    }
}

The Apple docs say

"Present the user interface by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, present the user interface using a popover. Doing so is valid only if the sourceType property of the image picker controller is set to UIImagePickerControllerSourceTypeCamera."

That says the exact opposite from how it behaves?!? You CANT present UIImagePickerControllerSourceTypeCamera from a popover and you CANT present UIImagePickerControllerSourceTypePhotoLibrary and UIImagePickerControllerSourceTypeSavedPhotosAlbum modally.

Strange...

POST iOS 8: Try adding popOver controller in

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ }];

Reason : This is because in iOS 8, alert views and action sheets are actually presented view controllers (UIAlertController). So, if you're presenting a new view controller in response to an action from the UIAlertView, it's being presented while the UIAlertController is being dismissed. You need to do it on main queue without disturbing the navigation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!