如何捕获屏幕视图并共享它?

时间:2016-04-12 17:40:25

标签: ios swift xcode7 share

我在不同的地方遇到了一些帖子,但不认为它们完全涵盖了我想要的内容。

通过点击一个名为“分享”的栏按钮项目,我希望我的应用程序在应用程序中截取当前视图的屏幕截图,然后弹出共享窗口,与Twitter,Facebook和其他人共享。< / p>

1 个答案:

答案 0 :(得分:0)

您可以添加分享按钮

var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton

并填充共享选项

func shareButtonPressed(sender: AnyObject) {
    NSLog("shareButton pressed")
    let stringtoshare: String = "This is a string to share"

    //add current screen shot image to share
    let imagetoshare = captureScreen()

    let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
    let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo]
    self.presentViewController(activityVC, animated: TRUE, completion: { _ in })
}

//捕获屏幕截图

func captureScreen() -> UIImage
{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);

    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)

    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image
}

如果您在此发现任何问题,请告诉我,我无法测试此代码片段(因为我不在系统中),但这是我以前在应用中处理的方式。