gpt4 book ai didi

ios - 如何在 Swift 中保存 View 及其 subview 的屏幕截图?

转载 作者:IT王子 更新时间:2023-10-29 05:48:22 30 4
gpt4 key购买 nike

我想捕捉一个 View 及其 subview 。我正在使用此代码来捕获屏幕:

let view = self.faceTrackerContainerView

UIGraphicsBeginImageContext(CGSizeMake(view.bounds.size.width, view.bounds.size.height))
UIGraphicsGetCurrentContext()!
self.view?.drawViewHierarchyInRect(view.frame, afterScreenUpdates: true)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

这些代码捕获整个屏幕,包括 View 前面的按钮。我尝试了这些代码,但它只捕获了不包括其 subview 的主视图:

let view = self.faceTrackerContainerView
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, scale);

view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)

我也从这里尝试了 snapshotViewAfterScreenUpdates(true) 但我不知道如何实现它。我在 View 下方有一个 collectionView,在 View 上方有一个按钮(两者都不在 View 中)。第一个代码使用按钮、collectionView 和 subview 捕获所有内容。第二个代码捕获没有 subview 的 View 。我错过了什么?

最佳答案

这对我有用,

func screenShotMethod() {
//Create the UIImage
UIGraphicsBeginImageContext(faceTrackerContainerView.frame.size)
faceTrackerContainerView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

Ref

关于ios - 如何在 Swift 中保存 View 及其 subview 的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40188126/

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