11

カメラから画像をキャプチャするアプリケーションを作成しました。これは私のコードです

 -(IBAction) showCameraUI {
    BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
}

そして、キャプチャされた画像を取得するためにこのデリゲート メソッドを実装しました

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
}

ユーザーがコントローラーをキャンセルした場合にこのメソッドを実装しました

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

しかし、それはこの例外を示しています。関数showCameraUIの最後の行を実行した後、なぜそのような例外が表示されるのか、誰にも分かりますか?

UIStatusBarStyleBlackTranslucent is not available on this device. 2013-02-07 
10:06:06.976 CaptureImage[460:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be 
presented via UIPopoverController'
4

3 に答える 3

14

例外に関しては、エラーメッセージは非常に明確です。「iPadでは、UIImagePickerControllerはUIPopoverControllerを介して提示する必要があります」 iPadの場合、UIPopoverControllerを使用する代わりにで提示する必要があります[self presentModalViewController:picker animated:YES];。これで問題が解決するはずです。

例:-

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    self.popover = popover;
} else {
    [self presentModalViewController:picker animated:YES];
}

編集: @rmaddyが述べたように、カメラはモーダルで表示できます。上記は、の場合に適用されsourceTypeますUIImagePickerControllerSourceTypePhotoLibrary

于 2013-02-07T06:30:23.503 に答える
0

iOS11 の Swift の場合:

            videoEditor.modalPresentationStyle = .popover
            self.present(picker, animated: true)

            let popover = picker.popoverPresentationController
            popover?.sourceView = self.view

            // then config popover frame and arrow style here
于 2018-03-29T13:51:14.387 に答える