3

我真的很难弄清楚如何做到这一点。我有一个UIImagePickerController工作正常,但我想创建自己的CameraOverlayView,以便我可以自定义按钮。我似乎无法弄清楚如何首先创建叠加层。我已经尝试了很多教程,并且已经达到了以下内容,但是还差得很远。谁能给我指出一个简单易懂的说明?

  • 我创建了一个UIImagePickerController适用于标准控件的
  • 对于叠加层,我UIViewController在情节提要上创建了一个新的并将其链接到一个名为的新类ObViewControllerCameraOverlay
  • startCameraControllerFromViewController我使用以下方法隐藏标准控件

    cameraUI.showsCameraControls=否;

  • 然后显示cameraOverlay(不编译)

    obViewControllerCameraOverlay *overlay = [[obViewControllerCameraOverlay alloc]; cameraUI.cameraOverlayView = 覆盖;

我知道我做错了,但我不知道如何做到这一点。我也看过 Apple 的 PhotoPicker,但我就是不明白。

更新:

我现在在哪里 - 谁能告诉我这是否是正确的过程(我意识到需要对帧大小进行排序并且需要以某种方式添加自定义按钮):

“拍照”所在的 .m 文件UIButton

@property (nonatomic, strong) UIView *overlay;

- (IBAction)takePhoto:(id)sender
{
[self startCameraControllerFromViewController: self usingDelegate: self];
}


- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate
{
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil)) return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerCameraCaptureModePhoto];
cameraUI.showsCameraControls=NO;
overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
cameraUI.cameraOverlayView = overlay;
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
4

2 回答 2

2

overlay只是分配 - 没有初始化。它应该被初始化并且应该正确设置它的框架。

另外:overlayView应该是一个UIView *类型。如果你想传递UIViewController一个覆盖视图,你只需要传递它的view属性。

例如:

obViewControllerCameraOverlay *overlay = [storyboard instantiateViewControllerWithIdentifier: @"obViewControllerCameraOverlay"];

cameraUI.cameraOverlayView = overlay.view;

您当然应该根据您的项目和配置更改代码。

于 2013-12-15T13:05:23.230 回答
1

我已经很晚了,但这是我用来创建 UIImagePickerController 和 CameraOverlayView 的代码。如果您希望相机在视图控制器打开后立即出现,请确保像我一样在 viewDidAppear 中初始化 UIImagePickerController(viewDidLoad 不起作用)。

@interface CameraViewController ()
@property UIImagePickerController *PickerController;
@property CGFloat HeightOfButtons;
@end


- (UIView *)createCustomOverlayView
{

    // Main overlay view created
    UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];

    // Clear view (live camera feed) created and added to main overlay view
    // ------------------------------------------------------------------------
    UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
    clear_view.opaque = NO;
    clear_view.backgroundColor = [UIColor clearColor];
    [main_overlay_view addSubview:clear_view];
    // ------------------------------------------------------------------------


    // Creates two red buttons on the bottom of the view (on top of the live camera feed)
    // Then adds the buttons to the main overlay view
    // You can, of course, customize these buttons however you want
    // ------------------------------------------------------------------------
    for(int i = 0; i < 2; i++) {
        self.HeightOfButtons = 100;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        // when a button is touched, UIImagePickerController snaps a picture
        [button addTarget:self action:@selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
        [button setBackgroundColor:[UIColor redColor]];
        [main_overlay_view addSubview:button];
    }
    // ------------------------------------------------------------------------

    return main_overlay_view;
}

- (void)makeCustomCameraAppear
{
    self.PickerController = [[UIImagePickerController alloc] init];
    self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.PickerController.showsCameraControls = NO;
    self.PickerController.delegate = self;

    UIView *overlay_view = [self createCustomOverlayView];
    [self.PickerController setCameraOverlayView:overlay_view];

    [self presentViewController:self.PickerController animated:YES completion:NULL];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self makeCustomCameraAppear];
}
于 2014-07-23T22:33:57.147 回答