I would like to enable a feature on my phone that allows users to click share, and then they would be able to text their friends a message that by default included a picture of the app (right before the user hit share). I cannot figure out how to do this - and whenever I search on how to do it all I get are tutorials/answers on how to share images in the phone's photo library or by their camera. Any idea on how to do this!
Asked
Active
Viewed 9,378 times
2
-
1Screenshot feature is already there in all iphones. So take screenshot and share it. – KAR Apr 12 '17 at 05:18
3 Answers
8
// to create an image from screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// to share the image
var imagesToShare = [AnyObject]()
imagesToShare.append(image)
let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
presentViewController(activityViewController, animated: true, completion: nil)

Christian Abella
- 5,747
- 2
- 30
- 42
5
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Descartes
- 59
- 2
-
1Use this code and get the uiimage from it and then you can proceed to share. – Gowtham Namuri Apr 12 '17 at 05:34
2
To share your screen just before user hits Share , first you will be needing to change your screen's view into an image and sharing it.
To get change your view into an image you can add this extension in your code.
//UIView extension which converts the UIView into an image.
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Pass your ViewController's view there , for example :
let imageToShare = self.view.toImage()
let activityItems : NSMutableArray = []()
activityItems.addObject(imageToShare)
let activityVC = UIActivityViewController(activityItems:activityItems as [AnyObject] , applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
I hope this helps you out.

Pallavi Srikhakollu
- 588
- 6
- 17