iPad displaying a selected pic from a popover - ios

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.

Related

Warning: Attempt to present <UIImagePickerController: 0x7ca5dc00> on <UIViewController: 0x7b9cac00> which is already presenting (null)

I got this error in using iPad. But iPhone is worked. Please share the solution. My code is given below.
-(void)pickImageFromLibrary
{
UIImagePickerController *picker10 = [[UIImagePickerController alloc] init];
picker10.delegate = self;
picker10.allowsEditing = YES;
picker10.view.tag=100;
picker10.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker10 animated:YES completion:NULL];
}
you should try this code!
[self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
UIImagePickerCopntroller must be presented in popover in iPad. take a look at the following code for 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];
}
don't forget to add a strong property for the popover:
#property (nonatomic, strong) UIPopoverController *popOver;
here are the delegate methods for dismissing it:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
here is the link to the class reference for more info: Class Refference

Pass image from camera to view from storyboard

Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks

Passing an image from one view to other iOS

I have 2 UIViewControllers in my app.
In the 1st viewController, i have a UIButton in a toolbar.
When i click on the button, it displays me a UIActionSheet to select whether to take a picture from camera or from gallery.
When i choose, for example, taking picture from gallery and i choose a picture, it displays the picture in the 1st viewController.
What i want is to take a picture from gallery photos or from camera and to go to the 2nd viewController and display the image in the 2nd viewController.
I searched a lot and tried so much codes but it doesn't work.
Here is the code of ViewController.m
- (IBAction)PictureButton:(UIButton *)sender {
NSLog(#"Click button take picture in toolbar view 1");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Photo"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Picture From Camera", #"Picture From Gallery", nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex)
{
case 0: {
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1: {
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
In my second view i created a UIImageView here is my code in SecondViewController.h
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageButton;
#end
Can You help me please.
Sorry for my bad English and thanks
It seems you want to select image at FirstViewController and display it in SecondViewContoller. However, you did not do anything to switch to SecondViewController and pass your selected image to it in ViewController.m.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.imageButton.image = image;
[self.navigationController pushViewController:secondViewController 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.

ImagePicker in a popover does not hide

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];.

Resources