How to take a screenshot of a pickerViewController and UIView?

 ̄綄美尐妖づ 提交于 2019-12-01 13:01:12

问题


I have a TableView who presents a PickerViewController on a ViewController. The ViewController show some Images and a button than triggering an action. I want that this action takes a screenshot as the screenshot that is provided by Apple. Here is my code:

@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


回答1:


you should write a extension for view's screenshot.Extension's return value is UIImage.You can print UIImage to UIImageview. Also if you want to, you can call here a button action.

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

Enjoy it :)




回答2:


func ScreenCapturing() 
{
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
    view.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}


来源:https://stackoverflow.com/questions/42778797/how-to-take-a-screenshot-of-a-pickerviewcontroller-and-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!