2

I am using the below code to select image from Camera or Photo Library. In iOS 8, it is picking image nicely. But in iOS 9 . picker view displayed but not selecting image. Not even go back to the controller. Tap on picture does nothing. What am i doing wrong.

- (void)showImgaePickerViewForSourceType:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.sourceType = sourceType;
    imagePickerController.allowsEditing = YES;
    imagePickerController.delegate = self;
    self.imgPickerController = imagePickerController;

    if (IS_IPAD) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgPickerController];
            [popover presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
            self.popOver = popover;
        }];
    }else{
             [self presentViewController:self.imgPickerController animated:YES completion:NULL];
    }
}
jcaron
  • 17,302
  • 6
  • 32
  • 46
user2096064
  • 108
  • 1
  • 9

7 Answers7

2

UIPopoverController is deprecated in iOS 9. Instead you should be using UIViewController with a modalPresentationStyle set to UIModalPresentationPopover.

For example:

UIImagePickerController *imagePickerController = ...;
CGRect sourceRect = CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400);

imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:imagePickerController animated:YES completion:nil]; 
imagePickerController.popoverPresentationController.sourceRect = sourceRect;
imagePickerController.popoverPresentationController.sourceView = self.view;

Note: UIModalPresentationPopover was introduced in iOS 8.0, if you need to support prior to 8.0 then you will need some conditional checks to use your old code in iOS 7 and the above in iOS 8+.

Note 2: I believe the technique above is also smart enough to work out if it should modally present the picker instead of a popover on iPhones (via size classes) so I don't think you need to do your IS_IPAD check.

liamnichols
  • 12,419
  • 2
  • 43
  • 62
2

I found that my app was running good on other iPhones(whether iOS 8 or iOS 9). I was not able to pick image from any other app. Then i decided to reset my iPhone. Now everything is perfect. It was'nt the code issue. It was my iPhone issue.

user2096064
  • 108
  • 1
  • 9
1

Use this code it is working fine in ios 9.I am using action sheet to show 2 options to choose.

   -(IBAction)imagepickertapped:(id)sender
   {
       UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Take Photo" otherButtonTitles:@"Photo Library",nil];

       popup.tag =909;
       popup.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
      [popup showInView:self.view];

   }
   -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {


    if (actionSheet.tag ==909)
{

    if (buttonIndex == 0)
    {

        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController* picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            // picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];

            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:NULL];
        }
        else
        {


            UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [altnot show];

        }

    } else if (buttonIndex == 1) {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];

        picker.delegate = self;
        picker.editing = YES;
        picker.allowsEditing = YES;

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:NULL];
    }

}


 }

   - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo{


   [picker dismissViewControllerAnimated:YES completion:NULL];


imagedata = UIImageJPEGRepresentation(image, 0.3);


Userimage.image = image;


 }

For IPAD please refers this link

find here

Community
  • 1
  • 1
Anil solanki
  • 962
  • 4
  • 20
0

Follow the below coding

- (void)imagePicker
{

   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypeCamera;
   [self presentViewController:pickerTakePhoto animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
   picker.delegate = self;
   [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{
   [picker dismissViewControllerAnimated:YES completion:NULL];
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

Since action sheets are deprecated and UIAlertController needs to be used, I am writing a similar answer for the benefit of newbies:

    - (IBAction)attachImageTapped:(id)sender
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
                                                              style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                                                              {
                                                              UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                              picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                              picker.delegate = self;
                                                              [self presentViewController:picker animated:YES completion:NULL];
                                                              }

                                                          }];
    UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
                                                              style:UIAlertActionStyleDefault
                                                            handler:^(UIAlertAction * action) {
                                                                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                                picker.delegate = self;
                                                                [self presentViewController:picker animated:YES completion:NULL];
                                                            }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                         }];

    [alertController addAction:pickFromGallery];
    [alertController addAction:takeAPicture];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
Koushik Ravikumar
  • 664
  • 11
  • 26
0

please follow this below steps for picking the images from Library and you can take image from camera using "UIAlertController" you just put the below code in your Button click events

- (IBAction)yourButtonClickEvent:(id)sender {

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery =
[UIAlertAction actionWithTitle:@"Take a photo"
                         style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action) {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate = self;
                [self presentViewController:picker animated:YES completion:NULL];
                                                            }

                                                        }];
UIAlertAction* takeAPicture =
[UIAlertAction actionWithTitle:@"Choose from gallery"
                         style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action) {
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:NULL];
                                                     }];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                    style:UIAlertActionStyleCancel
                  handler:^(UIAlertAction * action) {
        }];

[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil];

}

then after you just put UIImagePicker delegate Method like this:

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info

{
UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
self.imgForUpaloadDocument.image = imagePicked;
[picker dismissViewControllerAnimated:YES completion:nil];

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
    UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil);
}
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  {
    [picker dismissViewControllerAnimated:YES completion:nil];  
  }
Hiren kanetiya
  • 251
  • 2
  • 16
0

with the above code I would like to add something which you might need to implement uiimage picker viewcontroller.

If you want to prevent the app from crashing, you need to provide a description for why you are accessing the camera, photo library, etc. This is new in iOS10.

Input the following into your Info.plist file.

Photo

Key: Privacy - Photo Library Usage Description Value: $(PRODUCT_NAME) photo use

Camera

Key: Privacy - Camera Usage Description Value: $(PRODUCT_NAME) camera use

Tanvir Nayem
  • 702
  • 10
  • 25