简体   繁体   English

xCode 创建 UIImagePickerController 并引用它

[英]xCode Creating UIImagePickerController and Referencing It

for my app I want to create a custom camera overlay and create a UIImagePickerController to start recording video.对于我的应用程序,我想创建一个自定义相机叠加层并创建一个 UIImagePickerController 来开始录制视频。 (below) (以下)

- (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];
}

as part of the cameraOverlayView I've instantiated a UIButton:作为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;
}

So how do I create an IBAction for this button which can then make the UIImagePickerController cameraUI start recording?那么如何为这个按钮创建一个IBAction ,然后让UIImagePickerController cameraUI开始录制?

If somebody could also explain to me how the IBActions are called from specific buttons only that would also be amazing because that seems like black magic to me?如果有人还可以向我解释如何仅从特定按钮调用 IBActions,那也太棒了,因为这对我来说似乎是黑魔法?

You can add target like this,你可以像这样添加目标,

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

and you action method shoulbe like this你的行动方法应该是这样的

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

//handle your task on click here

}

second thing why you are adding layer on it?第二件事你为什么要在上面添加图层? you can use default video recording from imagePicker by setting mediatype like您可以通过设置媒体类型来使用 imagePicker 中的默认视频录制

cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

Update accroding to comment根据评论更新

you can use on button click,您可以在按钮单击时使用,

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

Update 2 :更新2

in your .h file,在您的 .h 文件中,

@property UIImagePickerController *cameraUI;

then in your method RecordVideo use like,然后在你的方法RecordVideo使用像,

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

 //etc.....

By this way you can use camaraUI in whole class by self通过这种方式,您可以使用camaraUI在全班self

hope this will help :)希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM