无法使用自定义相机拍摄照片

时间:2018-01-25 16:23:37

标签: swift avfoundation ios-camera

我正在尝试显示从我的自定义相机拍摄的图像(从类似于类似菜单的菜单,您向右滑动以打开左侧的相机)。我将代码更新为3.0,但现在它给了我这个错误: 'AVCapturePhotoOutput'没有成员'captureStillImageAsynchronouslyFromConnection',我找不到修复方法。     覆盖func viewWillAppear(_ animated:Bool){         super.viewWillAppear(动画)

    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSession.Preset.hd1920x1080

    let backCamera = AVCaptureDevice.default(for: AVMediaType.video)

    do {
        let input = try AVCaptureDeviceInput(device: backCamera!)

        if (captureSession?.canAddInput(input) != nil) {
            captureSession?.addInput(input)

            stillImageOutput = AVCapturePhotoOutput()

            captureSession?.addOutput(stillImageOutput!)
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
            previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
            cameraView.layer.addSublayer(previewLayer!)
            captureSession?.startRunning()
        }

    } catch {
        print("Something is wrong with the camera")
    }

}
func didPressTakePhoto() {
        if let videoConnection = stillImageOutput?.connection(with: AVMediaType.video) {
            videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
                (sampleBuffer, error) in

                if sampleBuffer != nil {
                    var imageData = AVCaptureStillImageOutput.jsegStillImageNSDataRepresentation(sampleBuffer)
                    var dataProfider = CGDataProviderCreateWithCFData(imageData)
                    var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                    var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

                    self.tempImageView.image = image
                    self.tempImageView.isHidden = true
                }


            })
        }
    }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    didPressTakePhoto()
}

我注意到不推荐使用stillImageOutput,现在使用了AVCapturePhotoOutput。 在3.0中使用AVCapturePhotoOutput异步编写captureStillImage的正确方法是什么?

我看到了这个问题的其他答案,但没有一个对我有用。 我想要的只是拍摄照片(动作),预览照片(我可以接受它并在我接受后做其他逻辑)。

1 个答案:

答案 0 :(得分:1)

let photoSettings = AVCapturePhotoSettings()
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .auto
stillImageOutput?.capturePhoto(with: photoSettings, delegate: self)

extension YourViewController: AVCapturePhotoCaptureDelegate {
    func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, ..., error: Error?) {
        guard error == nil, let photoSampleBuffer = photoSampleBuffer else { return }
        guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { return }
        let capturedImage = UIImage(data: imageData, scale: 1.0)
        ...
    }
}

手动输入,请原谅任何拼写错误。