如何在分享按钮中获取并显示屏幕截图?

时间:2017-08-16 16:52:38

标签: ios swift share screenshot

下面我尝试编码,因此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)  
}

1 个答案:

答案 0 :(得分: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)
    }
}