如何截取pickerViewController和UIView的截图?

时间:2017-03-14 06:05:19

标签: ios swift uiimagepickercontroller

我有TableViewPickerViewController上提出ViewController。 ViewController显示一些图像和按钮而不是触发动作。我希望这个动作将截图作为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

2 个答案:

答案 0 :(得分:1)

你应该为view的截图编写一个扩展名.Extension的返回值是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!
        }
    }

享受它:)

答案 1 :(得分:1)

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