UIImagePickerController not presenting in iOS 8

后端 未结 12 1550
借酒劲吻你
借酒劲吻你 2020-12-12 16:39

Is anyone else having an issue with UIImagePickerController in iOS 8? The method below works perfectly well in iOS 7 on an iPad, but I get the following error

相关标签:
12条回答
  • 2020-12-12 16:59

    I went through a lot of pain coming up with a solution which works on both iPad and iPhone, this is the final code which some of it comes from comments of other people: the code has some bugs but it's a very good place to start :)

    definitions :

    __weak IBOutlet UIButton *attachButton;
    UIImage *image;
    

    button's action :

        - (IBAction)doAttach:(id)sender {
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Select image from" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From library",@"From camera", nil] ;
        [action showInView:self.view];
      }
    
    
    
    #pragma mark - ActionSheet delegates
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if( buttonIndex == 1 ) {
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
            if(authStatus == AVAuthorizationStatusAuthorized)
            {
                NSLog(@"%@", @"You have camera access");
            }
            else if(authStatus == AVAuthorizationStatusDenied)
            {
                NSLog(@"%@", @"Denied camera access");
    
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    if(granted){
                        NSLog(@"Granted access to %@", AVMediaTypeVideo);
                    } else {
                        [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@“no camera access“
                                                                                       message: @“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
                                                                                preferredStyle:UIAlertControllerStyleAlert];
    
                        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@“ok” style:UIAlertActionStyleDefault
                                                                              handler:^(UIAlertAction * action) {
                                                                              }];
                        [alert addAction:defaultAction];
    
                        [self presentViewController:alert animated:YES completion:nil];
    
    
                        NSLog(@"Not granted access to %@", AVMediaTypeVideo);
                        return ;
                    }
                }];
            }
            else if(authStatus == AVAuthorizationStatusRestricted)
            {
                [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@“no camera access“
                                                                                       message: @“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
                                                                                preferredStyle:UIAlertControllerStyleAlert];
    
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@“ok” style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {
                                                                      }];
                [alert addAction:defaultAction];
    
                [self presentViewController:alert animated:YES completion:nil];
    
    
                NSLog(@"%@", @"Restricted, normally won't happen");
            }
            else if(authStatus == AVAuthorizationStatusNotDetermined)
            {
                NSLog(@"%@", @"Camera access not determined. Ask for permission.");
    
                [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                    if(granted){
                        NSLog(@"Granted access to %@", AVMediaTypeVideo);
                    } else {
                        NSLog(@"Not granted access to %@", AVMediaTypeVideo);
                        return ;
                    }
                }];
            }
            else
            {
                [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@“No camera access“
                                                                               message: @“error accusing camera”
                                                                        preferredStyle:UIAlertControllerStyleAlert];
    
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@“ok” style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {
                                                                      }];
                [alert addAction:defaultAction];
    
                [self presentViewController:alert animated:YES completion:nil];
    
    
                return;
                //NSLog(@"%@", @"Camera access unknown error.");
            }
    
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    
    
                UIImagePickerController *pickerView =[[UIImagePickerController alloc]init];
                pickerView.allowsEditing = YES;
                pickerView.delegate = self;
                pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    
                if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    
                    [ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
    
                    pickerView.modalPresentationStyle = UIModalPresentationPopover;
                    UIPopoverPresentationController *popPC = pickerView.popoverPresentationController;
                    popPC.sourceView = attachButton;
                    popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
                    [self presentViewController:pickerView animated:YES completion:nil];
                } else {
                    [self presentModalViewController:pickerView animated:YES ];
                }
            }
    
        }else if( buttonIndex == 0 ) {
    
            ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
            switch (status) {
                case ALAuthorizationStatusRestricted:
                case ALAuthorizationStatusDenied:
                {
                    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@“no access to library”
                                                                                   message: @“if you wish to access photos in this app go to settings -> appName-> and turn on photos .”
                                                                            preferredStyle:UIAlertControllerStyleAlert];
    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@“ok” style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {
                                                                          }];
                    [alert addAction:defaultAction];
    
                    [self presentViewController:alert animated:YES completion:nil];
    
                }
                    break;
    
                default:
                {
                    UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
                    pickerView.allowsEditing = YES;
                    pickerView.delegate = self;
    
                    [pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    
    
                    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    
                        [ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
    
                        pickerView.modalPresentationStyle = UIModalPresentationPopover;
                        UIPopoverPresentationController *popup = pickerView.popoverPresentationController;
                        popup.sourceView = attachButton;
                        popup.permittedArrowDirections = UIPopoverArrowDirectionAny;
                        [self presentViewController:pickerView animated:YES completion:nil];
                    } else {
                        [self presentModalViewController:pickerView animated:YES ];
                    }
                }
                    break;
            }
    
    
    
        }
    }
    
    #pragma mark - PickerDelegates
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
        [self dismissModalViewControllerAnimated:true];
    
        UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
        image = img;
    
    }
    
    0 讨论(0)
  • 2020-12-12 17:01

    performSelector:withObject:afterDelay solved my problem.

    also didDismissWithButtonIndex do the trick.

    Max

    0 讨论(0)
  • 2020-12-12 17:02

    Here is a solution that worked for me

    if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
    {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    
            [self presentViewController:cameraUI animated:NO completion:nil];
        }];
    
    }
    else{
    
        [controller presentViewController:cameraUI animated:NO completion:nil];
    }
    

    Remember to alloc cameraUI

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    

    Build and Go!

    0 讨论(0)
  • 2020-12-12 17:03

    You can dismiss the presented view controller (if any) by using

    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
    

    This worked for me.

    0 讨论(0)
  • 2020-12-12 17:06

    On iOS 8 you should use the new API:

    if (SYSTEM_VERSION_IOS_8) {
        self.imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController *popPC = self.imagePickerController.popoverPresentationController;
        popPC.barButtonItem = self.popoverItem;
        popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
        [self presentViewController:self.imagePickerController animated:YES completion:nil]
    }
    

    I recommend you watch the 2014 WWDC session 228 a look in side presentation controllers

    0 讨论(0)
  • 2020-12-12 17:06

    Here's a Xamarin solution. What worked for me was to add my actions to a Dismissed event handler.

    this.btnPhoto.TouchUpInside += (sender, e) =>
    {
        actionSheet = new UIActionSheet ("Add Photo");
        actionSheet.AddButton ("Take Photo");
        actionSheet.AddButton ("Select from Library");
        actionSheet.AddButton ("Cancel");
        actionSheet.DestructiveButtonIndex = -1; // red
        actionSheet.CancelButtonIndex = 3;  // black
        actionSheet.Clicked += delegate(object a, UIButtonEventArgs b)
        {
            actionSheet.Dismissed += (object aSender, UIButtonEventArgs dismissArgs) => 
            {
                switch (dismissArgs.ButtonIndex)
                {
                    case 0:
                        showCamera ();
                        break;
                    case 1:
                        showPhotoLibrary ();
                        break;
                }
            };
        };
        actionSheet.ShowInView (view);
    };
    
    0 讨论(0)
提交回复
热议问题