4

我正在使用带有自定义叠加层的 UIImagePickerController 在我的应用程序中录制视频。对于 UIImagePickerController 的实现,我使用了一个很棒的 Ray Wenderlich 教程中的代码。

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

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

所以“让记录”出现在日志上,但我收到 startVideoCapture 的 NSInvalidArgumentException 错误。我知道我尝试通过按下按钮开始视频捕获的方式明显有问题,但我无法弄清楚。在 iOS 上还是个新手,所以如果解决方案很简单,请原谅我!干杯,迈克

4

2 回答 2

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]; 
}
于 2013-07-22T14:51:29.907 回答
0

实际上,我刚刚在我打开的一些代码中快速对其进行了测试,sender对于您按下按钮的操作是UIBarButtonItem *. 所以你可以做几件事,你可以从根本上

UIBarButtonItem *senderButton = (UIBarButtonItem *)sender; 
if(senderButton.image == UIBarButtonSystemItemCamera)
{
   //Handle behaviour
}

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

于 2013-07-22T14:47:27.177 回答