Swift-Scheduled Timer无法按预期触发

时间:2018-10-01 15:04:38

标签: ios swift timer event-handling

我定义并触发了一个时间间隔为0.1秒(即每秒10次)的调度计时器,并将选择器设置为调用VNDetectFaceLandmarksRequest的完成处理程序的函数。

这在iPhone 6s和iPhone 7上可以正常工作。但是,当我在iPad mini 2上进行测试时,每秒只会调用一次请求。

是因为iPad mini 2太旧了,并且没有足够的系统资源来频繁调用它还是其他原因?

下面是相关代码:

var handler: VNImageRequestHandler?
var request: VNDetectFaceLandmarksRequest?    

faceTrackingTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.callHandler), userInfo: nil, repeats: true)   
faceTrackingTimer.fire()

// This is called about 30 times per second (frame capturing rate)
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .right, options: [:])
    request = VNDetectFaceLandmarksRequest { (request, error) in
    guard let results = request.results as? [VNFaceObservation] else { return }
        // process results here
    }
}

// Problem: this is called ONLY ONCE PER SECOND    
@objc func callHandler() {
    print("Will call handler ", Date()) 
    do {
    guard let handler = handler, let request = request else { return }
        try handler.perform([request])
    } catch let requestError {
        print(requestError.localizedDescription)
    }
}

0 个答案:

没有答案