PoKoBros
PoKoBros

Reputation: 721

How to make a camera overlay take pictures

I am trying to create an overlay for the camera in my app. I have succeeded in displaying a button to the screen, but I do not know what to set for the selector of the button. So I have a class called CameraOverlay and all it does is put a button on the screen. Then I have another class where I create the camera and handle everything when the camera is done taking the picture. What do I do to make it so that when the button is pressed in the CameraOverlay class the device takes the picture? Here is the code for the CameraOverlay class:

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //Create Button
        [self createCameraButton];

        self.backgroundColor = [UIColor clearColor];

        screenWidth = [UIScreen mainScreen].bounds.size.width;
        screenHeight = [UIScreen mainScreen].bounds.size.height;
        }
    return  self;
}

-(void)createCameraButton{
    self.cameraButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.cameraButton addTarget:self
           action:@selector(takePicture)
 forControlEvents:UIControlEventTouchUpInside];
    [self.cameraButton setBackgroundImage:[UIImage imageNamed:@"camera.png"]   forState:UIControlStateNormal];
    self.cameraButton.frame = CGRectMake(140, 500, 50, 50);
    [self addSubview:self.cameraButton];
}

Here is how I create the UIImagePickerController:

//create an overlay view instance
    CameraOverlayView *overlay = [[CameraOverlayView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    self.imagePicker = [[UIImagePickerController alloc]init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.navigationBarHidden = YES;
    self.imagePicker.toolbarHidden = YES;
    self.imagePicker.wantsFullScreenLayout = YES;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.videoMaximumDuration = 10;
    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
    self.imagePicker.showsCameraControls = NO;
    self.imagePicker.cameraOverlayView = overlay;
    [self presentViewController:self.imagePicker animated:NO completion:nil];

Upvotes: 0

Views: 648

Answers (1)

JerryCrowley
JerryCrowley

Reputation: 145

    -(void) takePicture:(id)sender{
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        picker.showsCameraControls = NO;

        [self presentViewController:picker animated:YES
                         completion:^ {
                             [picker takePicture];
                         }];}
    }

Upvotes: 0

Related Questions