iOS相机无法打开

时间:2016-02-02 15:54:01

标签: ios camera

几年前我创建了一个应用程序,让它完全没问题。我被要求复活它,并且发现了停机坪问题。选择按钮打开相机和/或照片库时,选择任一选项时会出现此消息:

Warning: Attempt to present <UIImagePickerController: 0x100a13400>  on :<ViewController: 0x100843a00> which is already presenting <UIAlertController:0x1001e8d40>`

我想知道iOS之间是否有变化,因为我需要修改我的代码?这是我现在拥有的:

- (IBAction)pickImage {

    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    if(status == AVAuthorizationStatusAuthorized) { // authorized

    }
    else if(status == AVAuthorizationStatusDenied){ // denied

    }
    else if(status == AVAuthorizationStatusRestricted){ // restricted


    }
    else if(status == AVAuthorizationStatusNotDetermined){ // not determined

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){ // Access has been granted ..do something

            } else { // Access denied ..do something

            }
        }];
    }
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Pick Image"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"from Camera", @"from Library", nil] ;
    [actionSheet showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
            [imagePickerController setDelegate:self];
            [self presentModalViewController:imagePickerController animated:YES];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                                message:@"Your device has no camera."
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alertView show];
        }
    }
    else if (buttonIndex == 1) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [imagePickerController setDelegate:self];
        [self presentModalViewController:imagePickerController animated:YES];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [imageView setImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

    [self dismissModalViewControllerAnimated:YES];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];

}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

无法使用该代码复制该错误消息,但是,您可以尝试使用UIAlertController替换UIActionSheet和U​​IAlertView。

自iOS 8.0起,这两个版本都已弃用,并已被UIAlertController取代。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

您可以将首选样式属性设置为&#34; UIAlertControllerStyleAlert&#34;或&#34; UIAlertControllerStyleActionSheet&#34;。