从ViewController拍照

时间:2014-06-17 17:05:13

标签: ios objective-c ipad

我正在使用scrollView制作应用。在一个屏幕上,我有很多UIPopovers。当您将页面滚动到新屏幕时,我希望能够在按下按钮后立即拍摄照片。 目前我有一个启动摄像头的按钮和一个作为subLayer添加的previewLayer

Current situation

代码:

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

}

正如您所看到的那样,相机处于纵向模式,我正在努力将其置于风景中。到目前为止没有运气。也很难找到按钮构建实际拍照。 如果有人能帮助我一点,我将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以转换预览图层,以使视频以正确的方向显示:

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