【问题标题】:xCode Creating UIImagePickerController and Referencing ItxCode 创建 UIImagePickerController 并引用它
【发布时间】:2016-04-21 12:05:17
【问题描述】:

对于我的应用,我想创建一个自定义的相机叠加层并创建一个 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,那也太棒了,因为这对我来说就像是黑魔法?

【问题讨论】:

    标签: ios xcode camera uiimagepickercontroller


    【解决方案1】:

    你可以像这样添加目标,

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

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

    -(void)captureBtnclick :(UIButton*)sender{
    
    //handle your task on click here
    
    }
    

    第二件事为什么要在上面添加图层?您可以通过设置 mediatype 来使用 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.....
    

    通过这种方式,您可以通过self在全班使用camaraUI

    希望这会有所帮助:)

    【讨论】:

    • 这很有帮助,谢谢!你对我应该如何让它开始 UIImagePickerController 录制有什么想法吗?
    • 为什么要添加overlayview?
    • 我需要添加自定义叠加层作为设计规范的一部分。我也只需要两个按钮(一个用于录制,一个用于接受录制)我明白这是这样做的方法吗?
    • 你不能使用打开带有媒体类型电影的uiimagepickercontroller时出现的默认录制按钮吗?
    • 我不这么认为。它必须是自定义图像,然后必须记录有限的时间(5 秒)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多