Screenshot in fast iOS?

Does anyone know how I can take a screenshot from my screen by code (quickly) and save it? Can I take a screenshot and save it as an image?

I looked and I see this code, but I can’t use it (I think) because it does nothing ..

var screen:UIScreen = UIScreen.mainScreen() snapshotVieww = screen.snapshotViewAfterScreenUpdates(false) UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0); snapshotVieww.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); provino = UIImageView(image: image) 

Thank you first!

+18
ios iphone image swift screenshot
Aug 22 '14 at 10:12
source share
3 answers

With Swift 4 / iOS 10.3, you can choose one of the following methods to solve your problem.




1. Take a screenshot of the view controller view

The following code shows how to take a screenshot and save it in the device’s photo album:

 import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage UIGraphicsBeginImageContextWithOptions(view.frame.size, true, 0) guard let context = UIGraphicsGetCurrentContext() else { return } view.layer.render(in: context) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return } UIGraphicsEndImageContext() //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } } 

Please note that the result of this code will be a .JPG image. Also note that the navigation bar and status bar will not be displayed in the final image.

Since iOS 10, as an alternative to the previous code, you can use the following code:

 import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage let renderer = UIGraphicsImageRenderer(size: view.frame.size) let image = renderer.image(actions: { context in view.layer.render(in: context.cgContext) }) //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } } 



2. Take a screenshot of the iPhone window

If you want to take a screenshot that includes a navigation bar (but not a status bar), you can use the following code:

 import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage guard let layer = UIApplication.shared.keyWindow?.layer else { return } UIGraphicsBeginImageContextWithOptions(layer.frame.size, true, 0) guard let context = UIGraphicsGetCurrentContext() else { return } layer.render(in: context) guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return } UIGraphicsEndImageContext() //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } } 

Since iOS 10, as an alternative to the previous code, you can use the following code:

 import UIKit class ViewController: UIViewController { /* ... */ @IBAction func screenshot(_ sender: UIBarButtonItem) { //Create the UIImage guard let layer = UIApplication.shared.keyWindow?.layer else { return } let renderer = UIGraphicsImageRenderer(size: layer.frame.size) let image = renderer.image(actions: { context in layer.render(in: context.cgContext) }) //Save it to the camera roll UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) } } 



Reminder

Starting with iOS 10, to prevent the crash of your application when calling the screenshot(_:) method, you need to add the NSPhotoLibraryUsageDescription key to the Info.plist project file:

 <key>NSPhotoLibraryUsageDescription</key> <string>Some description to explain why access is required</string> 
+31
Aug 22 '14 at 11:10
source share
β€” -

Only these few lines of code will get screenShot from View: (just tested)

  UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0); self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.imgView.image = image; 
+8
Aug 22 '14 at 10:47
source share

// A screenshot that can be shared on facebook or twitter, here's what ...

 func shareButtonClickedToTwitter(){ UIGraphicsBeginImageContextWithOptions(CGSizeMake(320,320), false, 0) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext(); self.view?.drawViewHierarchyInRect(CGRectMake(-30, -30, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true) var screenShot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.vc.showTWShare("I scored \(score) in Beanystalk can you do better? Available in App Store..", shareImage: screenShot)} func shareButtonClickedToFaceboook() { UIGraphicsBeginImageContextWithOptions(CGSizeMake(320,320), false, 0) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext(); self.view?.drawViewHierarchyInRect(CGRectMake(-30, -30, self.frame.size.width, self.frame.size.height), afterScreenUpdates: true) var screenShot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.vc.showFbShare("I got \(score) Points whilst playing BeanyStalk! Can you beat me?..", shareImageF: screenShot) } 

// function for sharing facebook.

  func showFbShare(messageFB: String , shareImageF: UIImage) { println(messageFB) if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){ var fbSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook) fbSheet.completionHandler = { result in switch result { case SLComposeViewControllerResult.Cancelled: break case SLComposeViewControllerResult.Done: break } } fbSheet.addImage(shareImageF) fbSheet.setInitialText("\(messageFB) Click here to fun https://hereIsLink/") println(messageFB) self.presentViewController(fbSheet, animated: false, completion: { }) } else { var alert = UIAlertController(title: "Accounts", message: "Please login to a facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } } 

// Twitter Share Function

 func showTWShare(message: String , shareImage: UIImage) { println(message) if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){ var twSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter) twSheet.completionHandler = { result in switch result { case SLComposeViewControllerResult.Cancelled: break case SLComposeViewControllerResult.Done: break } } twSheet.setInitialText("\(message) Click here 2 fun https://hereIsLink/") //The default text in the tweet twSheet.addImage(shareImage) println(message) self.presentViewController(twSheet, animated: false, completion: { }) } else { var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } } 
+1
Jul 31 '15 at 6:02
source share



All Articles