如何在Swift 4中使用AVCapturePhotoOutput捕获图像

时间:2018-06-30 06:39:54

标签: swift

我正在使用AVCapturePhotoOutput构建相机应用程序,并且照片捕获成功运行。图像具有黑色背景。 有时会出现错误,例如:

-[[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]没有活动和启用的视频连接”

那我在做什么?预先感谢。

我的代码如下:

 @available(iOS 11.0, *)
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        let imageData = photo.fileDataRepresentation()
        if let data = imageData, let img = UIImage(data: data) {
            print(img)
            imgmatch = img
            print(isBlinking)
            DispatchQueue.main.async {
                self.notificationCenter.post(self.visageBlinkingNotification)

            }
        }
    }
    @available(iOS 10.0, *)
    func capturePhoto() {
        let cameraOutput = AVCapturePhotoOutput()
        if self.captureSession.canAddOutput(cameraOutput) {
            self.captureSession.addOutput(cameraOutput)
        }
        let settings = AVCapturePhotoSettings()
        let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
        let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                             kCVPixelBufferWidthKey as String: 1080,
                             kCVPixelBufferHeightKey as String: 1080]
        settings.previewPhotoFormat = previewFormat

        settings.isHighResolutionPhotoEnabled = true
        settings.isAutoStillImageStabilizationEnabled = true
        cameraOutput.isHighResolutionCaptureEnabled = true
        cameraOutput.isLivePhotoCaptureEnabled = cameraOutput.isLivePhotoCaptureSupported

        cameraOutput.capturePhoto(with: settings, delegate: self)



    }

0 个答案:

没有答案