【问题标题】:A function is not getting called inside a delegate method - IOS/Swift没有在委托方法中调用函数 - IOS/Swift
【发布时间】:2016-05-26 15:21:44
【问题描述】:

我正在开发一个带有自定义相机的应用程序,并且我使用了 captureOutput 方法(比如说 A),它是 AVCaptureVideoDataOutputSampleBufferDelegate 的委托方法。

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

当我在 A 中调用 B 时,它不会被调用。

这是我的代码

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

    setTimeout(1) { () -> Void in         
        //piece of code i want to run               
    }
}

func setTimeout(delay:NSTimeInterval, block:()->Void) -> NSTimer {
    return NSTimer.scheduledTimerWithTimeInterval(delay, target: NSBlockOperation(block: block), selector: "main", userInfo: nil, repeats: false);
}

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

已编辑: 对我的代码不起作用的原因感到困惑。有什么想法吗?

【问题讨论】:

    标签: ios swift nstimer capture-output


    【解决方案1】:

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

    //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
        }
    
    }
    

    【讨论】:

    • 感谢您的快速回复。我会试试你的解决方案。知道我的代码有什么问题吗?
    • @GMHSJ 不知道,对不起。
    • 此解决方案在这种情况下也不起作用。它在 viewDidLoad() 中运行良好,但在我想要的方法中却没有。感谢您的回复。
    【解决方案2】:

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

    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
        });
    }
    

    【讨论】:

    • 根据我的经验,必须始终从主队列调用“NSTimer.scheduledTimerWithTimeInterval(...)”。这就是它在这里工作的原因,因为您使用的是 dispatch_get_main_queue()。
    • 太棒了。我也想过,但没有尝试,因为我找到了一个更简单的解决方案。会试试看。感谢您分享您的经验。
    猜你喜欢
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多