How to use UIImagePickerController in iPad?
Hi i am working on a universal application (iPhone/iPad). one feature is that i have to select a photo from album and show it on UIImageView.
Now problem is that it is working good on iPhone but when i try to open photo album it crashes. my code in action sheet delegate is this:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
}
if (buttonIndex == 1)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
else {
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
}
else{
if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
}
if (buttonIndex == 1)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
else {
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
}
}
can any body help me out? i have checked on stackOverflow and also googled it but in vain.
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)
}
}