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