Popover not showing iPad photo library - ios

I am trying to write an app that will allow the user to select an image from the photo library on the iPad. I have done exactly as sources online say it should be done, however when I click the button only the arrow is showed on screen and nothing else. The popover which should show with the images does not show. Could anyone please tell me why this is happening? Here is my code:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[picker setAllowsEditing:YES];
popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
[popoverController presentPopoverFromRect:self.view.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

I know it is to to late to answer but if you're already interested on it, this may work for you:
In the .h file, gives this delegates:
#interface ViewController : UIViewController <UIPopoverControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
create the popover as a property:
#property (nonatomic, strong) UIPopoverController *popoverController;
and finally in the .m file:
-(IBAction)showPhotoLibray:(id)sender
{
BOOL hasGellery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGellery ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.theViewObject frame]
fromView:[self.theViewObject superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
the self.theViewObject is an outlet of the object view controller that is calling the method, for example an UIButton

Related

How to set popover to fullscreen in ios

I want to make a popover to fullscreen
popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:CGRectMake(0, 0, 0, 0) inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUnknown animated:YES];
who know how to do it in ios7/ios8
well anyway if your problem is it's not working on a phone then add a category like this
#interface UIPopoverController (phone)
+ (BOOL)_popoversDisabled;
#end
#implementation UIPopoverController (phone)
+ (BOOL)_popoversDisabled {
return NO;
}
#end
then doing a popover like this seems just fine. so again, not sure what your actual problem is (if any)
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePicker.allowsEditing = NO;
UIPopoverController * popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0, 0, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUnknown animated:YES];

Can't present imagepicker immediately

I have a Tab bar controller, when I tap the third tab bar button I present a UIViewcontroller. In the viewWillAppear of this vc I'm presenting a UIImagepickerController that works fine. The problem is I can't display it on the screen immediately when I open the view. First the vc shows up and after 0.4-0.5 sec the image picker. So I would like to present the image picker first and present the vc after the user took an image. I tried to call the picker from viewDidLoad and viewWillAppear too, but nothing changed.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (imagePickerWasPresented == NO)
{
imagePickerWasPresented = YES;
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
Am I calling it in a wrong place?
I had the same problem - instead of calling the VC and then the UIImagePicker, call the UIImagePicker directly.
When you are done taking a picture/video:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {}
you will go to this standard delegate method, call the VC from here. This way you will immediately go to the ImagePicker and only have a transition would you choose to do something with the taken content afterwards which is less frustrating/ugly.
No, your calling it in an okay place, that's just how iOS does it; if you present multiple modals on top of each other, one gets presented after the other, including the animation. A solution that would work for your problem is to present a UINavigationController instead of your UIViewController. Set the navigation controller up to have ViewController as the root viewcontroller, but also push your imagepickercontroller onto the stack. Present this navigationcontroller and it should go right to your imagepickercontroller. Otherwise, try presenting both uiviewcontroller and imagepickercontroller with animation set to NO and see if that works.
try this to see how close that gets you then adapt to your needs. when i gave it a quick test it seemed to do what you were asking.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
UIViewController * vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor whiteColor];
UINavigationController * navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:vc animated:NO];
UITabBarController * tabBarController = [[UITabBarController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:navigationController, nil];
tabBarController.viewControllers = controllers;
tabBarController.delegate = self;
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
[vc presentViewController:imagePicker animated:NO completion:nil];
return YES;
}
UIImage as a PopOver
Gallery mode:
BOOL hasGallery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGalleryt ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
Camera mode:
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.imageView frame]
fromView:[self.imageView superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
Remember to give to the .h File #interface the delegates, like this:
#interface the UIViewController: UIViewController <UIPopoverControllerDelegate, UIImagePickerControllerDelegate>

UIImagePickerController inside popoverview causing error

I am trying to create a UIPopoverView that contains a UIImagePickerController for taking photos only. I have attempted to create something like this when I press a button on my toolbar. The method looks like this.
- (void) cameraButtonSelected
{
// create picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// create popover
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:picker];
[popOver presentPopoverFromRect:CGRectMake(10, 30, 200, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
the issue being this procduces the following error
Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.'
I am not sure exactly what this means so was hoping I could get some help on the matter.
Try to create UIPopoverController as public, I mean declare it in .h file and then use it.
Such like
#property (nonatomic, strong) UIPopoverController * popOver;
And then use it in .m file, such like....
.
.
.
.
self.popOver = [[UIPopoverController alloc] initWithContentViewController:picker];
[self.popOver presentPopoverFromRect:CGRectMake(10, 30, 200, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
UIPopoverController should always be held in an instance variable.
Please refer this UIPopovercontroller dealloc reached while popover is still visible
Declare your UIPopover as a property.
Like:
#property (nonatomic, strong) UIPopoverController *popOver;
Use it like:
self.popOver = [[UIPopoverController alloc] initWithContentViewController:picker];
[self.popOver presentPopoverFromRect:CGRectMake(10, 30, 200, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

UIPopoverController for iPad "must not be called with `nil`" error

I am trying to make an iPhone app work in an iPad but the UIPopoverController is
comming back with error.
- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
if (self.editing) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
} else {
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}
}else{
if (self.editing){
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
self.popover.delegate =self;
[popover release];
}else{
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}}}
The error I am getting is:
'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with nil.'
Anyone available to give me a hand on this code, I have looked on the internet for solutions and samples but can not seem to make it work.
Thank you.
___ added to original question_____
I am adding the recipePhotoViewController here, I am assuming that ImageView manipulation is the same for iPhone and iPad.
my.h File
#class Recipe;
#interface RecipePhotoViewController : UIViewController {
#private
Recipe *recipe;
UIImageView *imageView;
}
#property(nonatomic, retain) Recipe *recipe;
#property(nonatomic, retain) UIImageView *imageView;
#end
Here is my .m file
#implementation RecipePhotoViewController
#synthesize recipe;
#synthesize imageView;
- (void)loadView {
self.title = #"Photo";
imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
self.view = imageView; }
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:#"image"];}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
} #end
You are initializing the popover controller in a wrong way:
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
You should pass the controller you would like to display inside of the popover -- not the popover itself (which is nil since you have not yet initialised it)!
Maybe this would do it for you?
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
If this is correct, then you should also present the pop over you have just created: – presentPopoverFromRect:inView:permittedArrowDirections:animated:
-- e.g.:
[self.popover presentPopoverFromRect:sender.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
where sender is the argument to :
- (IBAction)photoTapped1:(id)sender {
The content of your popover which you store in the variable popover is obviously nil. I can't see it ever being created in the code you provided.
Maybe you intended to present the Recipe photo controller as the content of the popover. In that case you would do something like
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:recipePhotoViewController];
problem is in this line of your code ,
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
try like this
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 260)];
popoverContent.view = popoverView;
//Add what ever you want popoverView
self.popover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
self.popover.delegate =self;

how to open an image picker on ipad just in code

How can I create the image picker in code?
I use iOS 6.0 , with ARC, for ipad.
I would like to able to select the picture and somehow get UIImage of the selected image.
i did add delegates:
enter code here
in the viewDidLoad method did
enter code hereimagePicker = [[UIImagePickerController alloc] init];
and in the button method for click i have put
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
the crash happens in
[self presentModalViewController:imagePicker animated:YES];
There are some tutorials here http://www.raywenderlich.com/130/how-to-write-a-custom-image-picker-like-uiimagepicker and http://mobileorchard.com/ios-advanced-programming-the-image-picker/ that might help you
I have similar code in one of my projects, but I had
[self presentViewController:imagePicker animated:YES completion:nil];
instead of
[self presentModalViewController:imagePicker animated:YES];
Try that and see if it works. Note the presentViewController rather than presentModalViewController.
Add a property in .h
#property (strong) UIPopoverController *pop;
In you r .m file under your button implimentation add something like:
if (self.pop) {
[self.pop dismissPopoverAnimated:YES];
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; //if you want to edit the image
self.pop=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
//choose the direction of the arrow for the popover
[self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Make sure you have your <UIPopoverControllerDelegate> delegate set in .h

Resources