IOS take Screenshot of full page and share in swift 4

IOS take Screenshot of full page and share in swift 4

1. Crate a swift file and paste these two as extension

import UIKit

extension UIApplication {

    var screenShot: UIImage?  {
        return keyWindow?.layer.screenShot
    }
}

extension CALayer {

    var screenShot: UIImage?  {
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
        if let context = UIGraphicsGetCurrentContext() {
            render(in: context)
            let screenshot = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return screenshot
        }
        return nil
    }
}

2 . How to use :

We can use as UIApplication.shared.screenShot . If we want to set screenshot at a imageview then –

imageView.image = UIApplication.shared.screenShot

3 .Share screenshot

 let imageToShare = [UIApplication.shared.screenShot]
                    let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
                    activityViewController.popoverPresentationController?.sourceView = self.view
                    
                   
                    activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
                    self.present(activityViewController, animated: true, completion: nil)

Leave a Reply

Your email address will not be published. Required fields are marked *