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

标签 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)  
}

最佳答案

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

关于ios - 如何在分享按钮中获取和显示屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719113/

相关文章:

ios - 加大节点的攻丝面积。 ios swift

ios - 如何将 Storyboard链接到 View Controller

android - 是否可以在网络上运行 eclipse IDE,以便其他用户可以与您一起工作?喜欢谷歌文档?

c - 如何与其他进程共享现有的 char *?

ios - JSON 字符串不会被解析

ios - 灰度 UIView。 View 未重绘

ios - 为什么这会在 Combine 中产生编译器错误?

ios - 如何使用 SwiftUI 复制外观粗糙的笔记应用程序背景?

swift - 错误“"' ClassName -> () -> ClassName'没有名为 'memberName' 的成员”是什么意思?

javascript - 在 window.location 中使用 JS 变量