简体   繁体   English

如何在使用移动视觉人脸检测器时点击 UIView 进行捕捉

[英]How to tap on UIView to capture while using mobile vision face detector

I am trying to capture still image while tap on UIView.我试图在点击 UIView 时捕获静止图像。

Project from: https://github.com/googlesamples/ios-vision项目来自: https : //github.com/googlesamples/ios-vision

FaceDetectorDemo → FaceDetector → CameraViewController.m FaceDetectorDemo → FaceDetector → CameraViewController.m

I converted the Face detector project from Objective-C to Swift, but I need to add an additional feature which user tap on the screen to capture but just couldn't figure it out.我将人脸检测器项目从 Objective-C 转换为 Swift,但我需要添加一个附加功能,用户点击屏幕进行捕获但无法弄清楚。

My code:我的代码:

@IBOutlet weak var placeholder: UIView! 
var stillImageOutput = AVCaptureStillImageOutput()

for face in faces
{
  //somewhere in here called faceDetected() method
}

func faceDetected() -> Void
{
    let tapped = UITapGestureRecognizer(target:self,action:#selector(saveToCamera))      
    placeholder.addGestureRecognizer(tapped)
    placeholder.isUserInteractionEnabled = true
}

@objc func saveToCamera(_ sender: UIGestureRecognizer)
{
    if let videoConnection = stillImageOutput.connection(with: AVMediaType.video) {
        stillImageOutput.captureStillImageAsynchronously(from: videoConnection) {
            (imageDataSampleBuffer, error) -> Void in
            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer!)
            UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData!)!, nil, nil, nil)
        }
    }
}

It doesn't seem run into the saveToCamera while tapping on the UIView .在点击UIView时似乎没有遇到saveToCamera

  • The saveToCamera is not being called because when you have added placeholder.addGestureRecognizer(tap) instead of placeholder.addGestureRecognizer(tapped) , not tap but tapped saveToCamera没有被调用,因为当您添加了placeholder.addGestureRecognizer(tap)而不是placeholder.addGestureRecognizer(tapped) ,不是tap而是tapped
  • At selector the method mus be: saveToCamera(_:)在选择器中,方法必须是: saveToCamera(_:)

this is the full code.这是完整的代码。 It works.有用。

  func faceDetected() {
        let tapped = UITapGestureRecognizer(target:self,action:#selector(self.saveToCamera(_:)))
        placeholder.addGestureRecognizer(tapped)
        placeholder.isUserInteractionEnabled = true
    }



    @objc func saveToCamera(_ sender: UIGestureRecognizer) {

        if let videoConnection = stillImageOutput.connection(with: AVMediaType.video) {
            stillImageOutput.captureStillImageAsynchronously(from: videoConnection) {
                (imageDataSampleBuffer, error) -> Void in
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer!)
                UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData!)!, nil, nil, nil)
            }
        }
    }

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

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