2017-03-14 4 views
0

TableView私はViewControllerPickerViewControllerを提示しています。 ViewControllerは、アクションをトリガーするよりもいくつかのイメージとボタンを表示します。私はこのアクションがアップルが提供するスクリーンショットのスクリーンショットを取ることを望んでいます。ここ は私のコードです:pickerViewControllerとUIViewのスクリーンショットを取る方法は?

@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

+0

共有スクリーンショット。 –

+0

「UIView screenshot」を検索してください。 –

+0

@RajeshkumarR画像を共有しました。 –

答えて

1

ビューのscreenshot.Extensionの戻り値のための拡張機能を記述する必要がありUIImage.YouがUIImageviewにUIImageを印刷することができます。また、あなたがしたい場合は、ここでボタンアクションと呼ぶことができます。

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

それをお楽しみください:)ビューコントローラの

+0

それは働いていない...私は同じ結果を得た。 –

+0

私はFacebookの共有プロジェクトを使用しています。コードは動作しません@MarcoGonzalez –

+0

コードはスクリーンショットを作ることができますが、View Controllerだけです。View ControllerとPickerViewControllerのスクリーンショットが必要です。私は元の質問に写真を掲載します。 –

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

ポスターと他の人があなたの解決策を理解できるように説明をお願いします。 –

+0

これは機能しません。私は同じ結果を得た。 =( –

関連する問題