2017-08-16 2 views
1

以下、私はscreenShot関数が私のviewControllerの完全なスクリーンショットをとるようにコーディングしようとしました。どのようにスクリーンショットを撮ってsharePressedアクションのactivityItemsに入れることができるかを把握しようとしているので、共有しようとするとスクリーンショットが表示されます。共有ボタンでスクリーンショットを取得して表示するにはどうすればよいですか?

func captureScreen() -> UIImage? { 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale) 
    view.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 


@IBAction func sharePressed(_ sender: Any) { 

    let activityVC = UIActivityViewController(activityItems: [""], applicationActivities: nil) 
    activityVC.popoverPresentationController?.sourceView = self.view 

    self.present(activityVC, animated: true, completion: nil) 
} 
+2

このリンクを見 https://stackoverflow.com/questions/30876068/what-is-in-swift-telling-me/30876177 –

答えて

1
@IBAction func sharePressed(_ sender: Any) { 

    let imgScreenshot = captureScreen() 

    if let imgScreenshot = imgScreenshot { 
     let objectsToShare = ["Post message", imgScreenshot] as [Any] 
     let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 
     activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList] 
     self.present(activityVC, animated: true, completion: nil) 
    } 
} 
関連する問題