Yoko
Yoko

Reputation: 803

Take pictures from ViewController

I'm making an app with a scrollView. On one screen I have a lot of UIPopovers. When you scroll the page to a new screen I would like to have the ability to take a picture right away after the push of a button. Currently I have a button to start the camera and a previewLayer added as a subLayer

Current situation

Code:

- (IBAction)startCameraButtonPushed:(id)sender
{
    if (session.isRunning) {
        [startCamera setTitle:@"Start Camera" forState:UIControlStateNormal];
        previewLayer.hidden = YES;
        [session stopRunning];
        session = nil;
        previewLayer = nil;
    } else {
        [startCamera setTitle:@"Stop Camera" forState:UIControlStateNormal];
        session = [[AVCaptureSession alloc] init];
        AVCaptureOutput *output = [[AVCaptureStillImageOutput alloc] init];
        [session addOutput:output];
        NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        AVCaptureDevice *device = [possibleDevices objectAtIndex:0];
        NSError *error = nil;
        AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
        session.sessionPreset = AVCaptureSessionPresetPhoto;

        [session addInput:input];

        previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

        previewLayer.frame = CGRectMake(3202, 100, 800, 400);
        previewLayer.hidden = YES;
        // previewLayer.delegate = self;
        [self.scrollView.layer addSublayer:previewLayer];
        previewLayer.hidden = NO;
        [session startRunning];
    }

}

As you can see the camera is in portrait mode, and I'm trying really hard to get it in landscape. No luck so far. Also building in a button to actually take a picture is hard to find. If somebody could just help me a little, it would be greatly appreciated!

Upvotes: 0

Views: 55

Answers (1)

Austin
Austin

Reputation: 5655

You can transform the preview layer to get video to appear in the correct orientation:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(0);
} else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI/2);
} else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(-M_PI/2);
} else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI);
}

Upvotes: 1

Related Questions