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

最佳答案

@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)
    }
}

10-08 16:59