UIImagePickerController startVideoCapture与自定义叠加

时间:2013-07-22 14:14:26

标签: iphone ios uiimagepickercontroller

我正在使用带有自定义叠加层的UIImagePickerController在我的应用中录制视频。为了实现UIImagePickerController,我使用了great Ray Wenderlich tutorial.

中的代码

我隐藏了相机的控件,并创建了一个简单的自定义叠加视图。这工作和加载很好。 然后我为视图创建了一个工具栏和按钮,以录制视频:

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

// Displays a control that allows the user to choose movie capture
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;

cameraUI.delegate = delegate;

//Overlay view and toolbar setup 
// creating overlayView
UIView* overlayView = [[UIView alloc] initWithFrame:cameraUI.view.frame];
// letting png transparency be
float width = 320;
float AR = (4.0/3.0);

float toolbar_height = 480 - (AR*width);


UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, (AR*width), 320, toolbar_height)];
//toolBar.tintColor = [UIColor colorWithRed:(252/255.) green:(0/255.) blue:(48/255.) alpha:1];
toolBar.tintColor = [UIColor colorWithRed:(49/255.) green:(52/255.) blue:(49/255.) alpha:1];


UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *RecordBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(recordPressed:)];
UIBarButtonItem *CancelBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(imagePickerControllerDidCancel:)];
NSArray *buttons = [NSArray arrayWithObjects: CancelBarButtonItem, flexibleSpace, RecordBarButtonItem, flexibleSpace, nil];
[toolBar setItems: buttons animated:NO];

[overlayView addSubview:toolBar];
[overlayView.layer setOpaque:NO];
overlayView.opaque = NO;

cameraUI.showsCameraControls = NO;
cameraUI.cameraOverlayView = overlayView;

[controller presentViewController: cameraUI animated: YES completion:nil];
return YES;
}

我的按钮 recordBarButtonItem 调用 recordPressed ,其中包含:

- (void) recordPressed: (UIImagePickerController *) picker {
NSLog(@"lets record");
[picker startVideoCapture]; 
}

因此'let record'出现在日志中,但是我收到了startVideoCapture的NSInvalidArgumentException错误。我知道我试图通过按钮按下开始视频捕获的方式有明显错误,但我无法弄明白。如果解决方案很简单,在iOS上还是一个新手,请原谅我! 干杯, 麦克

2 个答案:

答案 0 :(得分:1)

问题是您附加到initWithBarButtonSystemItem调用的操作未通过UIImagePickerController实例。

我要做的是将UIImagePickerController设置为您的类的属性,并从您的操作中访问该属性,如下所示:

在你的.h:

@property (nonatomic, strong) UIImagePickerController *cameraUI;

在你的.m:

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
                           usingDelegate: (id <UIImagePickerControllerDelegate,
                                           UINavigationControllerDelegate>) delegate {

    ...

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

    ...

    UIBarButtonItem *RecordBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(recordPressed)]; // Removed the ':'

    ...
}


- (void) recordPressed {
    NSLog(@"lets record");
    [self.cameraUI startVideoCapture]; 
}

答案 1 :(得分:0)

实际上我只是在一些已打开的代码中快速测试了它,sender按下按钮的动作为UIBarButtonItem * UIBarButtonItem *senderButton = (UIBarButtonItem *)sender; if(senderButton.image == UIBarButtonSystemItemCamera) { //Handle behaviour } 。所以你可以做几件事,你可以找到

的根源
tag

或者为每个按钮设置{{1}}变量并跳过图像检查并查看标签,这可能会使逻辑更容易。