【问题标题】:How to get Camera working on iPad inside UIPopoverController?如何让相机在 UIPopoverController 内的 iPad 上工作?
【发布时间】:2013-02-18 21:28:02
【问题描述】:

我正在尝试编写使用相机或照片库拍照的 iOS 应用。这是我的第一个 iOS 应用程序,所以我没有经验。我找到了几个如何为 iPhone 执行此操作的示例,并将模态窗口调用 presentViewController 替换为 IUPopoverController。这适用于照片库 (imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary) 但对于相机 (imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera) 有以下错误:

Terminating app due to uncaught exception 'NSGenericException', reason: 'The content view controller argument must be the root of its associated view controller hierarchy.'

使用 Xcode 4.6 测试 iOS 6.1

我的代码:

- (void) useCamera:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;

        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            _popover = [[UIPopoverController alloc] initWithContentViewController: imagePicker];
            [_popover presentPopoverFromRect: _popoverCenter inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
        }
        else
        {
            [self presentViewController:imagePicker
                               animated:YES completion:nil];
        }

        _newMedia = YES;
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Camera failed to open"
                              message: @"Camera is not available"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
}

是否可以在 iPad 上的弹出框内显示相机? 谢谢。

【问题讨论】:

  • 您所拥有的应该可以正常工作。但是,大多数人宁愿您将相机呈现为全屏视图。虽然UIImagePickerViewController 的源类型不是相机,但必须在 iPad 上的弹出窗口中,而相机可以像在 iPhone 上一样显示 - 在全屏模式视图控制器中。
  • @rmaddy 如何在 iPad 上启动全屏模式视图控制器?由于 iPhone、iPad 和 iOS 版本之间的差异,网络上一片混乱

标签: objective-c xcode ipad uiimagepickercontroller uipopovercontroller


【解决方案1】:

以下回答了OP关于如何在iPad上全屏显示相机的评论。

将您的代码更改为:

- (void) useCamera:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;
        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;

        [self presentViewController:imagePicker
                           animated:YES completion:nil];

        _newMedia = YES;
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Camera failed to open"
                              message: @"Camera is not available"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
}

【讨论】:

  • 我现在无法在真实设备上尝试此操作,但在 iPad 模拟器上出现以下错误:*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'在 iPad 上,UIImagePickerController 必须是通过 UIPopoverController 呈现的
  • 只有在图像选择器源类型不是UIImagePickerControllerSourceTypeCamera 时才会出现该错误。对于非相机类型,您必须在 iPad 上使用弹出框。由于模拟器没有摄像头,所以无法在模拟器中测试我的答案。
  • 谢谢。我明天在设备上试试。
【解决方案2】:
-(void)showCamera
{
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType:
        UIImagePickerControllerSourceTypeCamera];

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;

cameraUI.delegate = delegate;

[controller presentModalViewController: cameraUI animated: YES];
}




- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {

[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}

// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
        didFinishPickingMediaWithInfo: (NSDictionary *) info {

NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;

// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
        == kCFCompareEqualTo) {

    editedImage = (UIImage *) [info objectForKey:
                UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:
                UIImagePickerControllerOriginalImage];

    if (editedImage) {
        imageToSave = editedImage;
    } else {
        imageToSave = originalImage;
    }

// Save the new image (original or edited) to the Camera Roll
    UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
}

// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) {

    NSString *moviePath = [[info objectForKey:
                UIImagePickerControllerMediaURL] path];

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
        UISaveVideoAtPathToSavedPhotosAlbum (
                moviePath, nil, nil, nil);
    }
}

[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2011-03-27
    • 2011-06-22
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多