6

我是一个早期的初学者,我正在尝试截取应用程序的屏幕截图并使用共享按钮进行共享。分享按钮有效,它让我分享我的初始文本和 URL,只是屏幕截图似乎甚至没有被截取。

经过几天的研究,我发现所有答案对我来说都可能太高级了,而且我可能一开始就缺少基础知识。

我不确定将截取屏幕截图的代码放在哪里,或者我必须设置什么才能使其工作。我也不知道如何将屏幕截图实现为图像。

这是我的分享按钮代码,效果很好:

func socialShare(sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
        var sharingItems = [AnyObject]()



        if let text = sharingText {
            sharingItems.append(text)
        }
        if let image = sharingImage {
            sharingItems.append(image)
        }
        if let url = sharingURL {
            sharingItems.append(url)
        }

        let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
        activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,
            UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }




    @IBAction func clickShare(sender: AnyObject) {


        socialShare("Text to share #Hashtag", sharingImage: UIImage(named: "image"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))
    }

这是我在各处找到的用于截屏的代码。但我不让它工作/不知道把它放在哪里,如何将它与 IBAction 连接以及如何将屏幕截图实现为图像。

//Generate the screenshot
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

我非常感谢能指导我了解缺失的基础知识的答案。提前非常感谢!

4

3 回答 3

16

快速截图(没有运营商状态栏)和分享代码示例:

    let bounds = UIScreen.mainScreen().bounds
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
    self.view.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let activityViewController = UIActivityViewController(activityItems: [img], applicationActivities: nil)
    self.presentViewController(activityViewController, animated: true, completion: nil)
于 2015-08-24T21:45:56.763 回答
6

在 Swift 3.1 中共享

@IBAction func shareButtonClicked(_ sender: Any) {
    //Set the default sharing message.
    let message = "Hello!"
    let link = NSURL(string: "http://stackoverflow.com/")
    // Screenshot:
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, true, 0.0)
    self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: false)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Set the link, message, image to share.
    if let link = link, let img = img {
        let objectsToShare = [message,link,img] as [Any]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.excludedActivityTypes = [UIActivityType.airDrop, UIActivityType.addToReadingList]
        self.present(activityVC, animated: true, completion: nil)
    }

}
于 2017-05-18T00:37:23.100 回答
2

斯威夫特 4 作品。

    let bounds = UIScreen.main.bounds
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
    self.view.drawHierarchy(in: bounds, afterScreenUpdates: false)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil)
    self.present(activityViewController, animated: true, completion: nil)
于 2020-01-04T15:34:51.830 回答