xCode 创建 UIImagePickerController 并引用它

xCode Creating UIImagePickerController and Referencing It

对于我的应用程序,我想创建一个自定义相机叠加层并创建一个 UIImagePickerController 来开始录制视频。 (下)

- (IBAction)RecordVideo:(UIButton *)sender {

    //initialise camera view
    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    cameraUI.showsCameraControls = NO;
    cameraUI.navigationBarHidden = YES;
    cameraUI.toolbarHidden = YES;

    OverlayView *overlay = [[OverlayView alloc]
                        initWithFrame:CGRectMake(0, 0, 20,20)];

    cameraUI.cameraOverlayView = overlay;

    [self presentViewController:cameraUI animated:YES completion:nil];
}

作为 cameraOverlayView 的一部分,我实例化了一个 UIButton:

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //clear the background color of the overlay
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];

        ... some overlay UI stuff


        UIButton *captureBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        UIImage *captureBtnImg = [UIImage imageNamed:@"captureBtn.png"];
        [captureBtn setBackgroundImage:captureBtnImg forState:UIControlStateNormal];
        captureBtn.frame = CGRectMake(self.frame.size.width/2 - 15,
                                      430,
                                      80,
                                      80);
        [self addSubview:captureBtn];
    }
    return self;
}

那么我如何为这个按钮创建一个 IBAction 然后让 UIImagePickerController cameraUI 开始录制?

如果有人还可以向我解释如何仅从特定按钮调用 IBAction,那也很棒,因为这对我来说就像黑魔法?

您可以这样添加目标,

[captureBtn addTarget:self action:@selector(captureBtnclick:) forControlEvents:UIControlEventTouchUpInside];

你的操作方法应该是这样的

-(void)captureBtnclick :(UIButton*)sender{

//handle your task on click here

}

第二件事为什么要在上面添加图层?您可以通过将媒体类型设置为

来使用来自 imagePicker 的默认视频录制
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

根据评论更新

您可以在按钮点击时使用,

 [picker startVideoCapture]; //in your case cameraUI i think

更新 2 :

在你的 .h 文件中,

@property UIImagePickerController *cameraUI;

然后在你的方法中 RecordVideo 使用 like,

self.cameraUI = [[UIImagePickerController alloc] init];
self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

 //etc.....

通过这种方式,您可以使用 camaraUI 整个 class by self

希望这会有所帮助:)