1

我正在尝试编写使用相机或照片库拍照的 iOS 应用程序。这是我的第一个 iOS 应用程序,所以我没有经验。我找到了几个如何为 iPhone 执行此操作的示例,并将模态窗口调用替换presentViewControllerIUPopoverController. 这适用于照片库 ( 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 上的弹出框内显示相机?谢谢。

4

2 回答 2

4

以下回答了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];
    }
}
于 2013-03-04T18:09:19.823 回答
0
-(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];
}
于 2013-12-11T09:41:01.757 回答