Image Picker on ipad - objective-c

I know image picker on the iPad needs to be a popover, and I keep trying but it won't work and crashes when I tap the button that calls the popover.
-(IBAction)addPhoto:(id)sender{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover=[[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:((UIButton *)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
// Delete any existing image.
NSManagedObject *oldImage = imageClass.image;
if (oldImage != nil) {
[imageClass.managedObjectContext deleteObject:oldImage];
}
// Create an image object for the new image.
NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:imageClass.managedObjectContext];
imageClass.image = myImage;
// Set the image for the image managed object.
[image setValue:selectedImage forKey:#"image"];
[self dismissViewControllerAnimated:YES completion:nil];
}

Based on the exception you are getting, the problem is that the sender for the addPhoto method is actually a UIBarButtonItem, not a UIButton. You need to change how the popover is displayed from using the button's frame to being displayed from the bar button.
-(IBAction)addPhoto:(UIBarButtonItem *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}

Try to create property to store Your popover controller or store content controller. Seems that one of view controllers releases right after end of method. Also, iOS 7 have awful behavior of UIImagePickerController in popover.
This is related question.

Related

nav bar nonresponsive after UIImagePickerController dismissed

On iOS 7 only, the navigation bar in my app does not respond to any touches after a UIImagePickerController is used and then dismissed (whether a pic has been selected or not). The screen below the navigation bar functions as normal, but it is now impossible to navigate Back in the app; the user is stuck on this screen.
I am launching the UIImagePickerController from code, though the rest of the app is laid out in storyboards.
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [NSArray arrayWithObject:(NSString *) kUTTypeImage];
mediaUI.allowsEditing = NO;
mediaUI.delegate = self;
[controller presentModalViewController: mediaUI animated: YES];
Thanks in advance for any help.
I hope you are performing these steps correctly!
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
And when you are calling
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
Cheers!
I noticed that the log was showing "Unbalanced calls to begin/end appearance transitions for" the launching controller. I was launching the image picker controller immediately from another controller when it appeared. This works OK on iOS 8 but there needs to be a delay on iOS 7. I fixed it by calling my method after a brief delay:
[self performSelector:#selector(takePicture) withObject:nil afterDelay:.1];

Segue not appearing after Image Picker

I am writing an iOS app where, in a view there is a button. Clicking on that button gets an action sheet which has options to pick image from camera or gallery. After the image is picked, it should be passed on to another view by manually calling a segue. The image picker appears and the prepare for segue code is executed, but the segue doesnt appear. There are no errors and I have checked all identifiers etc. Here is the code:
-(IBAction)showButtonClicked:(id)sender {
UIActionSheet *photoActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from Library", nil];
photoActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[photoActionSheet showInView:self.tabBarController.tabBar];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
break;
}
case 1:
{
[self dismissModalViewControllerAnimated:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
break;
}
default:
break;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"goToPhotoDetails"]){
FostoPhotoDetailsViewController *photoDetails = (FostoPhotoDetailsViewController *)segue.destinationViewController;
photoDetails.imageView.image = self.buttonImage1;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.buttonImage1 = image;
//FostoPhotoDetailsViewController *photoDetails = [[FostoPhotoDetailsViewController alloc] init];
//photoDetails.imageView.image = image;
[self dismissModalViewControllerAnimated:NO];
NSLog(#"Test");
[self performSegueWithIdentifier:#"goToPhotoDetails" sender:self];
}
Sounds like you are executing a segue with style push without being inside a UINavigationController.
When you call performSegueWithIdentifier:sender: it first correctly calls prepareForSegue:sender: then it tries to retrieve the current navigation controller and push the destination controller doing something like
[self.navigationController pushViewController:destinationController animated:YES];
But since you are not inside a UINavigationController the property navigationController is set to nil, causing the above call to fail silently.
Either change the display style of your segue to something other than push (for instance modal) or embed you controller in a UINavigationController.

Add A UIButton In A PopOverController - Xcode

I have an app which has a toolbar at the bottom with one of the options as Upload. On clicking on it, a PopOverController occurs which displays the list of photos to choose from.
After choosing a photo, I want to display a view (inside the PopOverController itself)which contains the photo alongwith the button called as Upload. On choosing my photo, currently my PopOverController is not getting dismissed. I have used the following lines of code :
-(IBAction)photolibrarypressed:(id)sender{
NSLog(#"hi");
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
imageView = [[UIImageView alloc] initWithFrame:[window bounds]];
[window addSubview:imageView];
imageView.hidden = YES;
[window makeKeyAndVisible];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
NSLog(#"hellow");
[self dismissModalViewControllerAnimated:YES];
// need to show the upload image button now
upload.hidden = NO;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
exit(0);
}
Can Someone help me to sort out the issue ??
Instead of calling
[self dismissModalViewControllerAnimated:YES];
call the same cancel method as int eh cancel method to handle dismissing it's modal view
[picker dismissModalViewControllerAnimated:YES]; // Dismisses modalView

UIPopoverController and UIImagePickerController crash

Here is the set up of my view:
When the UIBarButtonItem is clicked, it should bring up a UIImagePickerController. I have to do this using a UIPopoverController, which is invoked by clicking on the "reset" button, because it is necessary on the iPad. Here is my code:
-(IBAction) btnReset:(id)sender {
[self chooseImage];
}
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {
[self presentModalViewController:imagepicker animated:YES];
}
}
}
However, when this is called, the view crashes with the error:
'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
What am I doing wrong? Thank you in advance.
It looks like you are trying to create a popover on an item which is not on the view hierarchy. If this method is being invoked by your button then change the method header to -(void) chooseImage:(id)sender and try presenting the popover from the UIBarButton you have on your toolbar.
Also if you are using ARC (which it looks like you are) you have to hold on to your UIPopover otherwise it will be released when it is still required see this stack overflow post. You may already be doing this but I thought I would raise it as a I can't see if/how you have specified your popoverController.

UIImagePickerController custom overlay preview taken photo

I'm creating custom camera controls using the UIImagePickerController. The problem I'm having is this:
When the user takes a photo, I would like to show a preview of the photo they have just taken. I have managed to do this by displaying a UIImageView over the camera viewfinder. The only problem is that there is a visible delay before the imageview is shown. Any ideas? My code is shown below
- (void)viewDidLoad
{
[super viewDidLoad];
picker = [[UIImagePickerController alloc] init];
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
overlayViewController = [[CameraOverlayViewController alloc] initWithNibName:nil bundle:nil];
[picker setCameraOverlayView:overlayViewController.view];
[picker setShowsCameraControls:NO];
[picker setDelegate:self];
[self.view addSubview:picker.view];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
[overlayViewController.imgView setImage:img];
[overlayViewController.imgView setHidden:NO];
return;
}
The UIImagePickerController is a subclass of UINavigationController. You can create a custom preview view controller (just a view controller containing an image view and the cancel and use buttons) and push it on the image picker controller.