user1735714
user1735714

Reputation: 31

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

Upvotes: 1

Views: 2911

Answers (3)

JSA986
JSA986

Reputation: 5936

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

Upvotes: 0

mlstudent
mlstudent

Reputation: 959

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.

Upvotes: 0

Related Questions