Michael M
Michael M

Reputation: 1034

UIImagePickerController startVideoCapture with custom overlay

I am using UIImagePickerController with a custom overlay to record a video in my app. For the implementation of the UIImagePickerController, I have used the code from a great Ray Wenderlich tutorial.

I have hidden the controls for the camera, and created a simple custom overlay view. This has worked and loads fine. I have then created a toolbar and buttons for the view, to record the video:

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

My button recordBarButtonItem calls recordPressed which is given by:

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

So the 'lets record' appears on the log, but I receive an NSInvalidArgumentException error for the startVideoCapture. I know theres something obviously wrong with the way im trying to start the video capture through the button press, but I cant figure it out. Still quite a novice at iOS so forgive me if the solution is simple! Cheers, Mike

Upvotes: 4

Views: 3710

Answers (2)

Simon Germain
Simon Germain

Reputation: 6844

The problem is that the action you attach to the initWithBarButtonSystemItem call doesn't pass the UIImagePickerController instance along.

What I would do is set the UIImagePickerController as a property of your class and access that property from your action, like this:

In your .h:

@property (nonatomic, strong) UIImagePickerController *cameraUI;

In your .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]; 
}

Upvotes: 1

Nicholas Smith
Nicholas Smith

Reputation: 11764

Actually I've just quickly tested it in some code I've gotten open, the sender for your action on the button press is UIBarButtonItem *. So there's a couple of things you can do, you can either go down the root of

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

Or set the tag variable for each button and skip the image check and look at the tags instead, which might make the logic a bit easier.

Upvotes: 0

Related Questions