ios UIImagePickerController not working - ios

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
[imagePicker setAllowsEditing:YES] ;
[imagePicker setDelegate:self] ;
[imagePicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]] ;
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary] ;
[self presentViewController:imagePicker animated:YES completion:nil] ;

Step 1: Add Delegate
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Step 2: Initialize UIImagePicker on click button Action
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
Step 3:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Step 4: Dismiss controller
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Note: Post your Question with detail description

You are using UIImagePickerController as a variable not as an attribute or property, it means that it will be released at the end of your function. Set your UIImagePickerController as an attribute or property and then the delegate will work.

If you are using a UIPopoverPresentationController, make sure you dismiss the popover before presenting the imagePicker. Otherwise, it will not be shown.

Related

Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7

In iOS 8, When i click to open the camera from my ipad mini it gives warning
"Snapshotting a view that has not been rendered results in an empty snapshot"
I am using the below code to open camera from my device.
- (IBAction)takePhotograph:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.cmdTakePhotograph setImage:chosenImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
imageTaken = 1;
Compress = 1;
self.lblErrMsg.hidden = YES;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please suggest me solution to remove this warning.
Hope this helps you.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[imagePicker setShowsCameraControls:NO];
[self presentViewController:imagePicker animated:YES completion:^{
[imagePicker setShowsCameraControls:YES];
}];
}
else
{
[imagePicker setShowsCameraControls:YES];
[self presentModalViewController:imagePicker animated:YES];
}

iOS App - Trying to Get iOS photo from gallery but getting an error

I'm new to iOS coding, so my apologies in advance. I know this topic has been covered many times (I searched) however I can't seem to resolve my problem, which is why I am posting.
I am trying to access the iOS photo gallery but I keep getting two errors:
One is
'Application tried to present a nil modal view controller on target .'
Edit: The above error was fixed by initing the _picker in ChooseExsisting, as was suggested in the comments.
The other
[CameraController ChooseExsiting:]: unrecognized selector sent to instance 0x157e11330'
My code is as follows:
- (IBAction)ChooseExsiting {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:_picker animated:YES completion:NULL]; }
I imagine my ChooseExsisting code is incorrect. Would anyone have any suggestions? I would greatly appreciate it.
In your ChooseExisting method, you instantiate a controller into a local variable, but then call present with _picker property variable which is probably nil. Either present the controller from the local variable or init the property like in TakePhoto method.
EDIT: As for the second part, both your IBActions have a wrong method signature if you are connecting them to Tap handlers in the Storyboard for example. They should look like this:
- (IBAction) TakePhoto:(id)sender
- (IBAction) ChooseExsiting:(id)sender
Change your ChooseExisitng method to below one:
- (IBAction)ChooseExsiting:(id) sender {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:NULL]; }
or
- (IBAction)ChooseExsiting:(id) sender {
self.picker = [[UIImagePickerController alloc]
init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.picker animated:YES completion:NULL]; }
You can try this . It work for me
Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate)
2.Add below three methods in your controller
3. On button click call below methods
here choose photo for open gallary. so on button click call choose photo method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGSize newSize=CGSizeMake(150, 150);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_profile_pic.image = newImage;
[self dismissViewControllerAnimated:YES completion:nil];
isProfilePic=TRUE;
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController = imagePickerController;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) takePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
-(void) choosePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Instead of doing
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
} }
You should do this
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
if(!_picker){
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
[_picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
}else{
[self presentViewController:_picker animated:YES completion:NULL];
}}}
You should initialize _picker property before presenting it, right now you're trying to initialize local property of VC or you can also present self.picker instead of _picker in your code.

UIImagePickerController won't cancel, getting message about detached ViewControllers

I've not seen these two issues compounded together, so I figured I'd ask.
PhotoViewController
-(void)viewDidAppear:(BOOL)animated
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
if(!image) image = info[UIImagePickerControllerOriginalImage];
picker.delegate = self;
[self setPhotoForNextVC:image];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"givePhotoToGiveDetailsSegue" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
Selecting a photo works, but when I press cancel the UIImagePickerController keeps coming back up, and I get the error:
Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x10b38b560>.
I had faced this problem before as well.
For me it turned out to be that, especially in ViewDidAppear.
The OS doesn't like to put up new controllers right after another appears.
Make another method that handles this.
(void)bringUpCameraController {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentViewController:picker animated:YES completion:NULL];
}
My workaround :[self performSelector:<(bringUpCameraController)> withObject:<(nil)> afterDelay:<(2.0)>]
this will get rid of the message.
Look up the tree to PhotoViewController's hierarchy. It's probably not the root. I had this happen.
The error message is literal.

How to save the selected image in UIImagePicker controller?

I can take a photo and it shows in imageviewn but as soon as I leave detailviewn and go to back to detailviewn its disappear. How should the image always be saved?
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)selectPhoto:(UIButton *)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];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Check your IBOutlet Connection in xib file and You have to save the image taken in documents directory of your application , unless you cannot view the selected image

DismissViewControllerAnimated not working with navigation

In my application i have used the following code to implement after dismissing the view i have pushed to new view,When i tried to implement the view is not dismissing instead it overlapping.Here my code,
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
  {
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissViewControllerAnimated:YES completion:^{
cropingImageViewCon = [[CropingImageViewControl alloc]initWithNibName:#"CropingImageView" bundle:nil];
cropingImageViewCon.delegate = self;
cropingImageViewCon.originalImg = image;
[self.navigationController presentModalViewController:cropingImageViewCon animated:YES];
}];
}
Whats wrong with my code,Can any one please help out.
If you want to push into CropingImageViewControl then you are going wrong way,
Use
[self.navigationController pushViewController:yourViewCotrollerObject animated:YES];
instead of using presentModalViewController:

Resources