使用UIImagePickerController对开始/结束外观转换的不平衡调用

时间:2017-01-19 02:35:04

标签: ios objective-c

使用iOS 10+添加NSCameraUsageDescription所必需的 允许照片加密隐私相机使用要求。当我第一次启动以下代码时,这会导致弹出窗口出现:

弹出窗口似乎导致以下错误:

  

对于开始/结束外观转换的不平衡调用。

这是我试图执行的代码:

Z_HUFFMAN_ONLY

只要我在弹出窗口中选择确定以允许该应用使用相机,它就会崩溃。

如果我回到应用程序并再次尝试,因为弹出窗口没有出现,我的代码将会执行,我可以相应地获取图像。

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex{
    if (buttonIndex < 2) {

        UIImagePickerController *imagePickerController[[UIImagePickerController alloc] init];

        imagePickerController.mediaTypes = @[(__bridge NSString *)kUTTypeImage];

        imagePickerController.allowsEditing = YES;

        imagePickerController.delegate = self;

        if (buttonIndex == 0) {          
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        } else if ( buttonIndex == 1) {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        }

        [self presentViewController:imagePickerController animated:NO completion:nil];

    }
}

我知道之前已经问了很多次。任何想法我可能做错了什么?在此先感谢.......

1 个答案:

答案 0 :(得分:0)

从iOS 8.3弃用UIActionSheet时,您必须对所有提醒/操作表使用UIAlertController

请使用以下代码ActionSheet

UIAlertController* alert = [UIAlertController
                                alertControllerWithTitle:nil      //  Must be "nil", otherwise a blank title area will appear above our two buttons
                                message:nil
                                preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* button0 = [UIAlertAction
                              actionWithTitle:@"Cancel"
                              style:UIAlertActionStyleCancel
                              handler:^(UIAlertAction * action)
                              {
                                  //  UIAlertController will automatically dismiss the view
                              }];

UIAlertAction* button1 = [UIAlertAction
                              actionWithTitle:@"Camera"
                              style:UIAlertActionStyleDestructive
                              handler:^(UIAlertAction * action)
                              {
                                  //  The user tapped on "Camera"
                                  UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

                                  imagePickerController.allowsEditing = YES;
                                  imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                                  imagePickerController.delegate = self;
                                  [self presentViewController:imagePickerController animated:NO completion:nil];
                              }];

UIAlertAction* button2 = [UIAlertAction
                              actionWithTitle:@"Photo Library"
                              style:UIAlertActionStyleDestructive
                              handler:^(UIAlertAction * action)
                              {
                                  //  The user tapped on "Camera"
                                  UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

                                  imagePickerController.allowsEditing = YES;
                                  imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                  imagePickerController.delegate = self;
                                  [self presentViewController:imagePickerController animated:NO completion:nil];
                              }];
    [alert addAction:button0];
    [alert addAction:button1];
    [alert addAction:button2];
    [self presentViewController:alert animated:YES completion:nil];

使用相同的Imagepicker委托。

希望这会有所帮助。