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
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 :)
func ScreenCapturing()
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}