Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take screenshot of a UIView in swift?

I have a UIView named overView:

overView.frame = CGRectMake(self.view.frame.width/25, self.view.frame.height/25, self.view.frame.width/1.3, self.view.frame.height/1.2) 

I want to take a screenshot of this view only and not my entire screen. And make the screenshot of size:

 (CGSizeMake(2480,3508 ) 

Here is my code:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(2480,3508 ), false, 0); self.view.drawViewHierarchyInRect(CGRectMake(-self.view.frame.width/25, -self.view.frame.height/25,2480,3508), afterScreenUpdates: true) var image:UIImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext() 

The screenshot is of the required size however it takes the screenshot of the entire view instead of just "overView".

like image 227
Sameer Hussain Avatar asked Jul 23 '15 08:07

Sameer Hussain


People also ask

How do you take a screenshot in Swift?

scale UIGraphicsBeginImageContextWithOptions(layer. frame. size, false, scale); layer. render(in: UIGraphicsGetCurrentContext()!) let screenshot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return screenshot! }

Can we take screenshot in UiPath?

UiPath has an inbuilt Take Screenshot activity in UiPath. UIAutomation. Activities package. This activity can be used to take a screenshot of a specified UI element or a region.

How do you take a screenshot in JavaScript?

A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website.


2 Answers

For drawing of one view, just use this:

    // Begin context     UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)      // Draw view in that context     drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)      // And finally, get image     let image = UIGraphicsGetImageFromCurrentImageContext()     UIGraphicsEndImageContext() 

If you want to use it multiple times, probably extension would do the job:

//Swift4

extension UIView {      func takeScreenshot() -> UIImage {          // Begin context         UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)          // Draw view in that context         drawHierarchy(in: self.bounds, afterScreenUpdates: true)          // And finally, get image         let image = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()          if (image != nil)         {             return image!         }         return UIImage()     } } 

//Old Swift

extension UIView {      func takeScreenshot() -> UIImage {          // Begin context         UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale)          // Draw view in that context         drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)          // And finally, get image         let image = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()          return image     } } 

To explain what those parameters do:

UIGraphicsBeginImageContextWithOptions() creates a temporary rendering context into which the original is drawn. The first argument, size, is the target size of the scaled image. The second argument, isOpaque is used to determine whether an alpha channel is rendered. Setting this to false for images without transparency (i.e. an alpha channel) may result in an image with a pink hue. The third argument scale is the display scale factor. When set to 0.0, the scale factor of the main screen is used, which for Retina displays is 2.0 or higher (3.0 on the iPhone 6 Plus).

More about it here http://nshipster.com/image-resizing/

As for the draw call, Apple Docs explains it to detail here and here

like image 61
Jiri Trecak Avatar answered Oct 30 '22 12:10

Jiri Trecak


swift 4 and iOS 10+

extension UIView {    func screenshot() -> UIImage {     return UIGraphicsImageRenderer(size: bounds.size).image { _ in       drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)     }   }  } 
like image 34
Alessandro Avatar answered Oct 30 '22 10:10

Alessandro