每秒拍摄20张截图Swift?

时间:2017-05-10 17:22:31

标签: ios swift

我希望每秒拍摄20张截图3秒,所有这些都会添加到转换为视频的UIImage数组中。

以下是用于截取屏幕截图的代码:

func screenshot() {
        var imageSize = CGSize.zero

        let orientation = UIApplication.shared.statusBarOrientation
        if UIInterfaceOrientationIsPortrait(orientation) {
            imageSize = UIScreen.main.nativeBounds.size
        } else {
            imageSize = CGSize(width: UIScreen.main.nativeBounds.size.height, height: UIScreen.main.nativeBounds.size.width)
        }

        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
        for window in UIApplication.shared.windows {
            window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
        }

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        images.append(image!)

    }

我已经阅读了以下question,虽然它已经很安静了,但我确信在20FPS下可以完成3秒钟,但我不确定哪个会是实现它的最佳方式。

编辑: 使用UIScreen.main.nativeBounds.size而不是UIScreen.main.bounds.size,否则您将收到“错误”字样。图像大小,它可以安静的痛苦。如果“显示缩放”设置为“缩放”而不是标准信用,则为真:@ Christopher Pickslay

1 个答案:

答案 0 :(得分:1)

这有效:

var limit = 80 

func capture() {

     if limit > 0 {

     delay(0.03) {

     self.screenshot()

     self.capture()

   } else {

convertToVideo()

}

 func screenshot()  {

        var imageSize = CGSize.zero

        let orientation = UIApplication.shared.statusBarOrientation
        if UIInterfaceOrientationIsPortrait(orientation) {
            imageSize = UIScreen.main.nativeBounds.size
        } else {
            imageSize = CGSize(width: UIScreen.main.nativeBounds.size.height, height: UIScreen.main.nativeBounds.size.width)
        }

        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
        for window in UIApplication.shared.windows {
            window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
        }

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        images.append(image!)

        limit = limit - 1

    }

延迟是:

func delay(_ delay:Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(
        deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}