Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking Screenshot and share with share button

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!

like image 383
Ben Bar Avatar asked Aug 24 '15 18:08

Ben Bar


People also ask

How to take and share a screen shot in Windows?

How to take and share a screen shot in Windows. The active window is the program that you are currently using. To take a screen shot of only the active window you would press the ALT and Prt SC or ALT and PrintScreen at the same time. This will create a screen shot of the current window that you are using.

What happens when I press the key to take a screenshot?

When you press this key a copy of your current screen will be placed in the Clipboard of Windows. When you save that screen shot to an image file you will see an image of your entire screen at the time you pressed the key. An example screen is below: Notice that the image above is a screen shot of the entire screen at the time of the key press.

How to take a screenshot on iPhone?

1. Taking screenshots on iPhones X, XS or XR Hold down the Sleep/Wake button. After that, press the Volume Up button. The screen of your device will flash and the captured image will be saved as a photo on your device. 2. Taking screenshots on iPhones SE, iPhone 1-8, iPad and iPad Touch

How do I take a screen shot of the active window?

To take a screen shot of only the active window you would press the ALT and Prt SC or ALT and PrintScreen at the same time. This will create a screen shot of the current window that you are using.


1 Answers

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)
like image 109
rshev Avatar answered Nov 07 '22 06:11

rshev