Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未在委托方法内调用函数-IOS/Swift_Ios_Swift_Nstimer_Capture Output - Fatal编程技术网

未在委托方法内调用函数-IOS/Swift

未在委托方法内调用函数-IOS/Swift,ios,swift,nstimer,capture-output,Ios,Swift,Nstimer,Capture Output,我正在开发一个带有自定义摄像头的应用程序,在那里我使用了captureOutput方法(比如a),这是AVCaptureVideoDataOutputSampleBufferDelegate的委托方法 我使用了另一个函数(比如B),其行为类似于javascripts中的setTimeout 当我在A中呼叫B时,它不会被呼叫。 这是我的密码 当我在viewDidLoad()中调用setTimeout方法时,它工作正常。但是在这个特殊的方法中,它没有被调用。有人能给我一个解决办法吗。任何帮助都将不胜

我正在开发一个带有自定义摄像头的应用程序,在那里我使用了captureOutput方法(比如a),这是AVCaptureVideoDataOutputSampleBufferDelegate的委托方法

我使用了另一个函数(比如B),其行为类似于javascripts中的setTimeout

当我在A中呼叫B时,它不会被呼叫。

这是我的密码

当我在viewDidLoad()中调用setTimeout方法时,它工作正常。但是在这个特殊的方法中,它没有被调用。有人能给我一个解决办法吗。任何帮助都将不胜感激


编辑:混淆我的代码不工作的原因。有什么想法吗?

这可能更适合你的目的。它做你想做的事

//From https://gist.github.com/natecook1000/b0285b518576b22c4dc8 by natecook1000
extension NSTimer {
    /**
     Runs a closure after a specified time

     - parameter delay:   The time before the `handler` is called
     - parameter handler: What happens after `delay` has passed

     - returns: The NSTimer
     */
    static public func schedule(delay delay: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
        let fireDate = delay + CFAbsoluteTimeGetCurrent()
        let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0, handler)
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
        return timer
    }

    /**
     Runs a closure after a specified time, forever

     - parameter interval: The time before the `handler` is called
     - parameter handler:  What happens after `delay` has passed

     - returns: The NSTimer
     */
    static func schedule(repeatInterval interval: NSTimeInterval, handler: NSTimer! -> Void) -> NSTimer {
        let fireDate = interval + CFAbsoluteTimeGetCurrent()
        let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval, 0, 0, handler)
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
        return timer
    }

}

我能够找到一种方法来完成我的工作。但是找不到我以前的代码有什么问题。这就是它的样子

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    let seconds: Int64 = 1 * Int64(NSEC_PER_SEC);

    let time = dispatch_time(DISPATCH_TIME_NOW, seconds);

    dispatch_after(time, dispatch_get_main_queue(), {
        //piece of code i want to run
    });
}

谢谢你的快速回复。我会试试你的解决方案。你知道我的代码出了什么问题吗?@GMHSJ不知道,对不起。这个解决方案在这种情况下也不起作用。它在viewDidLoad()中工作正常,但在我想要的方法中没有。谢谢您的回复。根据我的经验,“NSTimer.scheduledTimerWithTimeInterval(…)”必须始终从主队列调用。这就是它在这里工作的原因,因为您使用的是dispatch\u get\u main\u queue()。太好了。我也这么想,但没有尝试,因为我找到了一个更简单的解决方案。我会试试看。谢谢分享你的经验。
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    let seconds: Int64 = 1 * Int64(NSEC_PER_SEC);

    let time = dispatch_time(DISPATCH_TIME_NOW, seconds);

    dispatch_after(time, dispatch_get_main_queue(), {
        //piece of code i want to run
    });
}