在点击UIView时尝试捕获静止图像。
项目来自: https://github.com/googlesamples/ios-vision
FaceDetectorDemo> FaceDetector> CameraViewController.m
我将人脸检测器项目从目标c转换为swift,但是我需要添加一个附加功能,用户可以在屏幕上点击以捕获它,但是无法弄清楚..
我的代码:
methods:{
focusChanged(){
console.log('focusChanged ');
},
}
在点击UIView时似乎没有碰到saveToCamera,我对IOS开发人员是相当陌生的,需要一些指导。先谢谢了。
答案 0 :(得分:1)
saveToCamera
未被调用,因为添加后
placeholder.addGestureRecognizer(tap)
代替
placeholder.addGestureRecognizer(tapped)
,不是tap
,而是tapped
saveToCamera(_:)
这是完整的代码。可以。
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)
}
}
}