gpt4 book ai didi

ios - 屏幕上未显示的 VC 的屏幕截图

转载 作者:行者123 更新时间:2023-11-30 12:43:58 25 4
gpt4 key购买 nike

我需要截取属于另一个不在屏幕上的 View Controller 的一部分的 View 的屏幕截图。

我相信我需要创建该 VC,用正确的数据填充它,“渲染它”,然后截取屏幕截图,我该怎么做?

这是我当前的代码,它从目标 VC 触发 viewDidLoad() 但生成空白图像:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ShareVC") as! ShareTransactionViewController
vc.transaction = self.transaction

vc.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: false)

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, 0.0)

let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let imageToShare = [ image ]

提前谢谢

最佳答案

经过一番工作后,我设法找到了一个解决方案,它可能不是最好的解决方案,因此如果有人有更好的方法,我将不胜感激。

我在当前 View Controller 中添加了一个 View ,以便我可以将目标 View 添加到其中。我捕获图像,然后删除添加的 View ,以便当前 VC 看起来与原来相同。

func share(sender:UIBarButtonItem){

// Create the VC that has the view we are going to share
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ShareVC") as! ShareTransactionViewController
vc.transaction = self.transaction

// Prepare the image context
UIGraphicsBeginImageContextWithOptions(self.snap.bounds.size, true, 0.0)

// Draw the view we want to share from the created VC into the current VC
vc.view.drawHierarchy(in: self.snap.bounds, afterScreenUpdates: true)

// Adds the view from the other vc to the snap of the current VC
self.snap.insertSubview(vc.view, at:0)

// Redraws all the views of this vc
self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)

// Defining the variable for activityItems
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let imageToShare = [ image ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)

// set up activity view controller
activityViewController.popoverPresentationController?.sourceView = self.view // so iPads won't crash

// Present the view controllet that shares the content
self.present(activityViewController, animated: true, completion: nil)

// Removes added subview in current VC
for view in self.snap.subviews {
view.removeFromSuperview()
}

}

关于ios - 屏幕上未显示的 VC 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885267/

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