我正在尝试编写使用相机或照片库拍照的 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 上的弹出框内显示相机?谢谢。