ImagePicker in a popover does not hide - ios

I have read that the iPad must use a UIPopoverController to view the PhotoLibrary, however, I have edited the code to make it, the popover shows but it does not hide when I choose a picture.
I found that it does not reach the didFinishpickingMediaWithInfo. Am I missing anything? here is my code
-(IBAction) ButtonClicked{
ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate=self;
popover = [[UIPopoverController alloc] initWithContentViewController:ipc];
[ipc release];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 800.0, 400.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
here:
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
and here:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishpickingMediaWithInfo:(NSDictionary *)info{
// TempImage is a UIImage instance
TempImg = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//bgImage is a UIImageView instance and it's connected in the IB
[bgImage setImage:TempImg];
// Dismiss UIImagePickerController and release it
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
}
I really need someone's help, I have already watched every youtube video, read every article on the internet and tried almost everything. I would really appreciate your help.

The first problem is that the method didFinishpickingMediaWithInfo is spelled wrong and so it won't get called. It should be didFinishPickingMediaWithInfo (uppercase P for Picking).
Second problem is calling dismiss on the parent or the picker will not hide the popover. Instead, try calling [popover dismissPopoverAnimated:YES];.

Related

How to dismiss UIImagePickerController on iPad

I am using a UIImagePickerController.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(0, 10, 500, 500) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
When I use any photo from gallery class delegate didFinishPickingMediaWithInfo is called.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"inside Delegate image pick");
[popover dismissPopoverAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
That is working fine, but I'm having trouble with the imagePickerControllerDidCancel delegate method.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(#"inside Cancel ");
[popover dismissPopoverAnimated:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
imagePickerControllerDidCancel is never called, and I don't know why. How do I dismiss the popover when the user clicks cancel?
If imagePickerControllerDidCancel does not get call it means you need to implement UIImagePickerControllerDelegate
Example
#interface ImageViewController : UIViewController<UIImagePickerControllerDelegate>

UIIMagePickerController dismissViewControllerAnimated:completion: not working

I'm trying to pick an image from UIImagePickerController and then set it inline inside UITextView. The code below is pretty straightforward. When i tap "act" button, it opens up UIImagePickerController and once it's done, the delegate method didFinishPickingMediaWithInfo gets triggered.
- (IBAction)act:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if(!self.imageView){
self.imageView = [[UIImageView alloc] initWithImage:chosenImage];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.imageView setFrame:CGRectMake(10.0, 10.0, 100.0f, 100.0f)];
[self.content addSubview:self.imageView];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
} else {
[self.imageView setImage:chosenImage];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
Here's the problem: when i try to dismiss UIImagePickerViewController using dismissViewController, it is just ignored. However, it works fine without the two lines where i'm setting the exclusionPaths. Also, this only happens when there's already text in the UITextView. If there's no text, I can do this as many times as I want and dismissViewController works fine. I've stepped through the code and the code actually does pass through the [picker dismissViewControllerAnimated: completion: line. Also picker at that point is indeed an instance of UIImagePickerController. Anyone have idea what is going on?
[UPDATE] I've actually trimmed down the code to the following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Basically I don't even set the imageView anymore. It's just that when I try to set the exclusionPaths of a UITextView--self.content--the dismissViewControllerAnimated:completion: doesn't work. There is no error in the console.
You have to call dismissViewConroller: on self and not picker.
Better yet, call it on picker.presentingViewController like this:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

UIPickerController crashes the app

I have a UIPickerController that gets your pictures and allows you to pick some of them though at the moment when I click the button to activate it the app crashes.
Here is the code that I am using for it:
in my ViewDidLoad method:
pickerController = [[UIImagePickerController alloc] init];
pickerController.allowsEditing = NO;
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
The function:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
patientPicture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageView *pictureView = (UIImageView *)[imageCell viewWithTag:777];
pictureView.image = patientPicture;
[_imgViewAdd reloadInputViews];
}
And it being called:
- (IBAction)addPicture:(id)sender {
[self presentViewController:pickerController animated:YES completion:nil];
}
It is wierd because I have recently changed my app to Ipad only though while it was in IPhone it worked fine. When you click the button in NSLog this error message crops up which I supose is something to do with it:
UIStatusBarStyleBlackTranslucent is not available on this device.
I suspect this is quite a common issue that people have
Thanks in advance
Try presenting in a popover...
pickerController = [[UIImagePickerController alloc] init];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.delegate = self;
and to present...
[popOverController presentPopoverFromRect:yourframe inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Using UIImagePickerController in UIView

I have button which when I click displays the camera (works perfectly)
- (IBAction)getPhoto:(id)sender
{
NSLog( #"Button clicked!" );
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else { // IF the device doesn't have a camera, so use the photos album
[imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}
[self addSubview:imagePicker.view];
}
After which I can take an image, etc, - But when I click "use" : nothing happens here is my method : the Image picker does not dismiss itslelf : the application simply does nothing, I'm trying to take the image I just took and display it inside a CGRect on screen .
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}
Even when I hit the cancel button without taking an image, the imagePicker (camera interface) does not dismiss itself
Any help, advice, or guidance would be very appreciated.
You shouldn't be doing the "addSubview" thing up there but instead do:
[self presentViewController:imagePicker animated:YES completion:nil];
Instead of adding the picker as a subview, you should add the picker to a UIPopoverController.
You can then display it from within your UIView subclass by using the presentPopoverFromRect:inView:permittedArrowDirections:animated: popover method like this:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
Then, you can use the delegate methods to dismiss the popover using:
[self.popover dismissPopoverAnimatedYES];
The is required by the official docs:
The table indicates that on iPad, if you specify a source type of UIImagePickerControllerSourceTypePhotoLibrary or
UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present
the image picker using a popover controller, as described in
“Presenting and Dismissing the Popover” in UIPopoverController Class
Reference. If you attempt to present an image picker modally
(full-screen) for choosing among saved pictures and movies, the system
raises an exception.
On iPad, if you specify a source type of
UIImagePickerControllerSourceTypeCamera, you can present the image
picker modally (full-screen) or by using a popover. However, Apple
recommends that you present the camera interface only full-screen.
Is the getPhoto: method in the UIView subclass?
If thats the case, then it would be better to:
Move the getPhoto: code out of the UIView subclass entirely, and put it in the view's parent view controller.
Make your UIButton a property of your UIView: #property (nonatomic, strong) UIButton *myButton;
In your view controller, set the button's target & action as follows:
[myView.myButton addTarget:self action:#selector(getPhoto:) forControlEvents:UIControlEventTouchUpInside];
Finally, in your getPhoto: method, call [self presentViewController:imagePicker animated:YES completion:NULL];
Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info code should also go in the view controller, changing self.patientImage.image = chosenImage; to myView.patientImage.image = chosenImage;
Dont know why the cancel button is not working.
But make this change it will select the image you are clicking.
If you want an cancel event add it by creating custom UIBarButtonand add it.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
[imagePicker.view removeFromSuperview];
}
Try:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker.view removeFromSuperview];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}
Remove the [self.addSubView:imagePicker.view]; and instead
[self presentViewController:imagePicker animated:YES completion:NULL];
You are dismissing the variable picker although picker is undefined. Instead put:
[self dismissModalViewControllerAnimated:YES completion:NULL];
and make sure its at the end of your method.

iPad displaying a selected pic from a popover

After researching about UIImagePickerController, I got this code to select an image from a popover and then display it in myParticularImageView.
This the ViewController.m:
#interface ViewController () {
UIImagePickerController *imagePickerController;
UIPopoverController *popover;
}
#end
- (IBAction)chooseImageButtonPressed:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover setDelegate:self];
[popover presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
//then to dismiss the popover and display pic
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[self myParticularImageView] setImage:image];
[popover dismissPopoverAnimated:YES];
}
#end
The problem I have is that when I tap on a pic from the popover nothing happens.
In .h I got:
:UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate>
What might cause my problem and how do I solve it?
You are not the image pickers delegate, which is why you are not hitting the delegate method.
Need to add: [imagePicker setDelegate:self]; at some point when creating it.

Resources