swift 截图并使用共享按钮共享

bq3bfh9z  于 2023-02-15  发布在  Swift
关注(0)|答案(3)|浏览(340)

我是一个早期的初学者,我正在尝试截图的应用程序,并分享它与分享按钮。分享按钮的工作,它让我分享我的初始文本和网址,只是截图似乎甚至没有采取。
经过几天的研究,我发现所有的答案对我来说可能都太高级了,我可能在一开始就错过了基础知识。
我不知道把截图的代码放在哪里,也不知道要设置什么才能使它工作,我也不知道如何将截图实现为图像。
这是我的共享按钮的代码,运行良好:

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()

我将非常感谢一个答案,引导我缺失的基础知识。提前感谢这么多!

jum4pzuy

jum4pzuy1#

快速截图(无运营商状态栏)和分享代码样本:

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)
ezykj2lf

ezykj2lf2#

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

}
w8ntj3qf

w8ntj3qf3#

斯威夫特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)

相关问题