gpt4 book ai didi

ios - 如何截取 pickerViewController 和 UIView 的屏幕截图?

转载 作者:行者123 更新时间:2023-11-28 12:28:17 24 4
gpt4 key购买 nike

我有一个 TableView,它在 ViewController 上显示一个 PickerViewController。 ViewController 显示一些图像和一个按钮而不是触发一个 Action 。我希望此操作将屏幕截图作为 Apple 提供的屏幕截图。这是我的代码:

@IBAction func share(){

UIGraphicsBeginImageContext(view.frame.size)
UIGraphicsBeginImageContext(thePicker.view.frame.size)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
thePicker.view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
present(activity, animated: true, completion: nil)
}

This image is the result of using the screenshot provided by Apple (side + home button) This image is result of my code

最佳答案

你应该为 View 的截图写一个扩展。扩展的返回值是UIImage。你可以将UIImage打印到UIImageview。此外,如果您愿意,可以在此处调用按钮操作。

import UIKit


class VideoContentController: UIViewController{
override func viewDidLoad()
{
super.viewDidLoad()
self.viewScreenShot()
}
func viewScreenShot()
{
let image = self.youtView.takeScreenShot()
self.yourImageView.image = image
}


}

extension UIView {

func takeScreenShot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)

drawHierarchy(in: self.bounds, afterScreenUpdates: true)

// old style: layer.renderInContext(UIGraphicsGetCurrentContext())

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}

好好享受吧:)

关于ios - 如何截取 pickerViewController 和 UIView 的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778797/

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