ios - 取消按钮在 UIImagePickerView 中不起作用

标签 ios objective-c uiimagepickercontroller

我在我的代码中使用UIImagePickerView,其中有三个按钮。一键拍照,二键选择照片,三键取消。代码如下:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?"
                                                                 delegate:self
                                                        cancelButtonTitle:@"Cancel"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Take Photo", @"Select Photo", nil];
        [actionSheet showInView:self.view];

当我单击“取消”按钮时,它不起作用。

我正在使用 UIImagePickerViewDelegate 方法,如下所示。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    @try {
        //set selected image in imageview by imagepickerview

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    imgProfilePic.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:nil];
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception.description);
    }
}

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    NSLog(@"@@@@");
//    [picker dismissViewControllerAnimated:YES completion:NULL];
//}

请检查我的 UIImagePickerView 代码,并为我提供正确代码的指导。

#pragma mark - Actionssheet delegate method for Image

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

    if(buttonIndex != 3)
    {

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;

        if(buttonIndex == 0)
        {
            NSLog(@"Choose Photo from Camera");

            //simulator has no camera so app will crash, below call just provide the alert that device has no camera.
            if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                      message:@"Device has no camera"
                                                                     delegate:nil
                                                            cancelButtonTitle:@"OK"
                                                            otherButtonTitles: nil];

                [myAlertView show];

            }
            else
            {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            }
        }
        if(buttonIndex == 1)
        {
            NSLog(@"Choose Photo from Gallary");
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        }

//        if (buttonIndex == 3)
//        {
//            NSLog(@"###");
//        }
//        
//        else
//        {
//            NSLog(@"Cancel the tab");
//            [picker dismissViewControllerAnimated:YES completion:NULL];
//
//        }
        [self presentViewController:picker animated:YES completion:nil];


    }
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception.description);

    }
}

最佳答案

我认为您是 stackOverflow 和 iOS 应用程序开发的新手。 UIActionsheetUIImagePickerController 都是不同的东西。因此,如果您要创建一个 actiohsheet,例如:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select the operation to proceed?"delegate:self
         cancelButtonTitle:@"Cancel"
         destructiveButtonTitle:nil otherButtonTitles:@"Take Photo", @"Select Photo", nil];
        [actionSheet showInView:self.view];

现在你可以像下面这样调用它的按钮方法:

   -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex==0)
            {
                   // take photo selected
            }
            else if (buttonIndex==1)
            {
                   // select photo selected
            }
            else
            {
                  // cancel button selected
            }
     }

您还可以使用 UIAlertController,它是 iOS8 中 UIAlertUIActionSheet 的替代品,因此您可以像下面这样使用:

UIAlertController* AlertSheet = [UIAlertController alertControllerWithTitle:@"New ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault //
                                                      handler:^(UIAlertAction * action) {
                                                          NSLog(@"Action");
                                                      }];
[AlertSheet addAction:defaultAction];

UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

}];
[AlertSheet addAction:cancleAction];
[self presentViewController:AlertSheet animated:YES completion:nil];

关于ios - 取消按钮在 UIImagePickerView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36123689/

相关文章:

objective-c - 将当前日期添加到日期选择器 objective-C 的前一天和后一天

iphone - 保存太长的录制视频时,应用程序崩溃

ios - 约束在 ios 中没有改变

ios - 解析聊天室 - 将 NS 数组转换为 NSMutableArray 以在 TableView 上显示

iphone - MFMailComposeViewController : how do I embed a clickable URL link into the email message body

iphone - 将 iPad 2 中的相机锁定为横向模式

ios - 如何从另一个应用程序启动 iOS 照片应用程序? ( swift )

iphone - UIRequiredDeviceCapabilities 排除没有后置摄像头的 16GB 第 5 代 iPod touch?

ios - PlacesAPI 的 OVER_QUERY_LIMIT 错误原因

ios - iPhone 项目在启动时崩溃