5

I am an early beginner, and i am trying to take a screenshot of the app and share it with a share button. The share button works, it lets me share my initial Text and URL, just the screenshot seems to not even be taken.

After days of research i found that all answers are likely too advanced for me and i am probably missing basics at the beginning.

I don't know for sure where to put the code that takes the screenshot, or what i have to set up to make it work. I also don't know how to implement the screenshot as the image.

This is my code for the share button, which works well:

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

And this is the code i found everywhere to take the screenshot. But i don't get it to work / don't know where to put it, how to connect it with the IBAction and how to implement the screenshot as the image.

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

I would very much appreciate an answer that guides me to the missing basics. Thanks so much in advance!

Ben Bar
  • 55
  • 1
  • 3

3 Answers3

15

Quick screenshot (without carrier status bar) and share code sample:

    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)
rshev
  • 3,780
  • 1
  • 21
  • 30
  • Wow thanks that totally did it! But now the Text and URL doesn't show up anymore, nowhere. Do you have any idea why? (Is there a way to post the entire code as of now again?) – Ben Bar Aug 24 '15 at 22:25
  • 1
    @BenBar I've just omitted adding text and URL in my example to quickly show you a proof of working concept. You can easily re-integrate those parts from your code (just add URL and text to `activityItems` array). Please accept the answer if I helped you. – rshev Aug 24 '15 at 22:37
  • Thanks so much! This seems to be a much better solution in any way. – Ben Bar Aug 25 '15 at 14:57
  • I've successfully used this code in my Xcode project, however: I've been trying to figure out what part needs changing for the status bar to be included? Plus, downloading to the photo library seems to be crashing the app atm. I see this answer is a little old, but still seems to be working for the most part. – Simon Apr 21 '17 at 17:09
6

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

}
Jayce
  • 377
  • 4
  • 6
  • In order to downloading to the photo library, you have to add NSPhotoLibraryUsageDescription into Info.plist [link from other answer](http://stackoverflow.com/questions/39519773/nsphotolibraryusagedescription-key-must-be-present-in-info-plist-to-use-camera-r) – Jayce May 18 '17 at 00:40
1

Swift 4 Works.

    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)
Mike
  • 91
  • 8