gpt4 book ai didi

ios - 每秒截取 20 张屏幕截图 Swift?

转载 作者:可可西里 更新时间:2023-11-01 02:04:54 27 4
gpt4 key购买 nike

我想每秒截取 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 否则你会得到“错误的”图像大小,这可能会很痛苦.如果 Display Zoom 设置为 Zoomed 而不是标准 Credit:@ Christopher Pickslay

最佳答案

这有效:

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

关于ios - 每秒截取 20 张屏幕截图 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43899040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com