获取iOS/swift中当前屏幕的屏幕截图以供共享

use*_*543 2 screenshot ios swift

我想在我的手机上启用一项允许用户点击共享的功能,然后他们就可以向他们的朋友发送一条消息,默认情况下包含应用的图片(在用户点击共享之前).我无法弄清楚如何做到这一点 - 每当我搜索如何做到这一切时,我得到的是关于如何在手机的照片库或他们的相机中共享图像的教程/答案.关于如何做到这一点的任何想法!

Chr*_*lla 8

//从截图中创建图像

UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)

//分享图片

var imagesToShare = [AnyObject]()
imagesToShare.append(image)

let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
presentViewController(activityViewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


小智 5

UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)