捕获图像并将其设置为UIImageView

捕获图像并将其设置为UIImageView

我试图捕获图像并将其设置为UIImageView,因此要创建相机,我具有以下代码:

class HomeController: BaseController, UIImagePickerControllerDelegate {

var detector: AFDXDetector?
var captureSession : AVCaptureSession?
var stillImageOutput : AVCapturePhotoOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
var camera : AVCaptureDevice!

@IBOutlet weak var cameraBtn: UIButton!
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var cameraImageView: UIImageView!

    override func viewDidLoad() {
      super.viewDidLoad()
      startCamera()
    }

    func startCamera() {

    do {

        captureSession = AVCaptureSession()
        camera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
        captureSession?.sessionPreset = AVCaptureSessionPreset1280x720

        let input = try AVCaptureDeviceInput(device: camera)

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

            if (captureSession?.canAddOutput(stillImageOutput))!{
                print("output added")
                captureSession?.canAddOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                cameraView.layer.addSublayer(previewLayer!)
                captureSession?.startRunning()
            }
        }

    } catch {

    }
}


@IBAction func cameraBtnPressed(_ sender: Any) {
   if (stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)) != nil
    {
        print("video connection detected")
    }
}
}

由于某些原因,尽管相机正在工作,但不会调用打印语句“检测到视频连接”

还有谁知道为什么吗?

最佳答案

在captureSession?.canAddOutput(stillImageOutput)的if语句内,将captureSession?.canAddOutput(stillImageOutput)更改为.addOutput

关于ios - 捕获图像并将其设置为UIImageView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42146129/

10-12 04:32