简体   繁体   English

捕获图像并将其设置为 UIImageView

[英]Capturing image and setting it to UIImageView

I am trying to capture an Image and set it to the UIImageView, hence to create the camera I have the following code:我正在尝试捕获图像并将其设置为 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")
    }
}
}

For some reason, the print statement "video connection detected" doesn't get called although the camera is working出于某种原因,尽管相机正在工作,但不会调用打印语句“检测到视频连接”

Does anyone else know why?有谁知道为什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM