如何使用AVCaptureSession拍照

时间:2013-10-31 02:34:05

标签: ios avcapturesession

我有一个将摄像机视图设置为子视图的代码,我需要的是当我点击一个按钮从这个AVCaptureSession拍摄照片并将其保存到照片库时,我怎么能得到这个?

Heres是我的代码:

  

AVCaptureSession * session = [[AVCaptureSession alloc] init];           AVCaptureOutput * output = [[AVCaptureStillImageOutput alloc] init];           [session addOutput:output];

    //Setup camera input
    NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    //You could check for front or back camera here, but for simplicity just grab the first device
    AVCaptureDevice *device = [possibleDevices objectAtIndex:1];

    NSError *error = nil;
    // create an input and add it to the session
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; //Handle errors

    //set the session preset
    session.sessionPreset = AVCaptureSessionPresetMedium; //Or other preset supported by the input device
    [session addInput:input];

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

    previewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI_2);
    //Set the preview layer frame
    previewLayer.frame = CGRectMake(45, 55, 512, 387);
    //Now you can add this layer to a view of your view controller
    [self.cameraPlace.layer addSublayer:previewLayer];
    [session startRunning];

0 个答案:

没有答案