0

I am trying to capture still image while tap on UIView.

Project from: https://github.com/googlesamples/ios-vision

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.

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Shyan
  • 1
  • 2
  • this is just the same question as https://stackoverflow.com/questions/36265659/how-capture-picture-while-mobile-vision-api-face-tracking , but in ios – Shyan Feb 11 '19 at 09:10

1 Answers1

2
  • The saveToCamera is not being called because when you have added placeholder.addGestureRecognizer(tap) instead of placeholder.addGestureRecognizer(tapped) , not tap but tapped
  • At selector the method mus be: 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)
            }
        }
    }
Enea Dume
  • 3,014
  • 3
  • 21
  • 36
  • Do you need the `self.` in `#selector(self.saveToCamera(_:))`? – trojanfoe Feb 11 '19 at 08:44
  • sorry for the mistake, it was actually placeholder.addGestureRecognizer(tapped) instead of placeholder.addGestureRecognizer(tap). I just forgot to change while uploading the question – Shyan Feb 11 '19 at 08:44
  • thanks enea. but it doesn't seem work to me when I apply it within the loop :/ – Shyan Feb 11 '19 at 09:07
  • you need to debug what happens in the loop. Maybe somethig goes wrong there – Enea Dume Feb 11 '19 at 09:17
  • @EneaDume if you have spare time to help, I actually just uploaded my project on Github https://github.com/shyanhua/facedetector .. btw many thanks for your advices – Shyan Feb 11 '19 at 10:39
  • your podfile is missing – Enea Dume Feb 11 '19 at 10:44