Getting a full screenshot of a UIScrollView in iOS 13
It's seems like iOS 13 bug but I found the solution in this answer.
The solution is I am removing scrollview from superview and then adding in with its old constraints after taking the screenshot. Yeah, it's stupid but it just works that way :(
scrollView.removeFromSuperview()
let print = scrollView.screenshot()
mainView.addSubview(scrollView)
scrollView.snp.makeConstraints { (make) in
make.top.equalTo(topView.snp.bottom)
make.left.bottom.right.equalToSuperview()
}
Get screenshot of current screen in iOS/swift for sharing
// 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)
How to take screenshot of a UIView in swift?
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
Save a screenshot as pdf or high resolution-image using Swift 5 xcode 13
Just convert your image to pdf via PDFKit
import PDFKit
let pdfPage = PDFPage(image: screenshotImage)
let fm = FileManager.default urls = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let savedPDF = urls!.appendingPathComponent("pdfPage.pdf") pdfPage?.document?.write(toFile: "pdfPage")
Take a full screenshot for all webview in swift
Try the following code after web view is loaded:
EDITED: run this code in simulator then check the screenshot.jpeg file in your documents app directory
class ViewController: UIViewController,UIWebViewDelegate {
let fileDirectory = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
var urlPath = "http://www.bbc.com/"
let requestUrl = NSURL(string: urlPath)
let request = NSURLRequest(URL: requestUrl!)
webView.loadRequest(request)
webView.delegate = self
}
func webViewDidFinishLoad(webView: UIWebView) {
if (!webView.loading){
var webViewFrame = webView.frame
webView.frame = CGRectMake(webViewFrame.origin.x, webViewFrame.origin.y, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height)
UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, false, 0);
self.webView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
var imageData = UIImageJPEGRepresentation(image, 0.7)
var currentFileName = "screenshot.jpeg"
var imageFilePath = fileDirectory[0].stringByAppendingPathComponent(currentFileName)
var imageFileURL = NSURL(fileURLWithPath: imageFilePath)
imageData.writeToURL(imageFileURL!, atomically: false)
webView.frame = webViewFrame
}
}
}
Related Topics
Draw Segments from a Circle or Donut
iOS Detect If User Is on an iPad
Uiview and Initwithframe and a Nib File. How to Get the Nib File Loaded
How to Recognize Swipe in All 4 Directions
iOS (Iphone, iPad, Ipodtouch) View Real-Time Console Log Terminal
iOS 7 Status Bar with Phonegap
Auto Layout Constraints Issue on iOS7 in Uitableviewcell
Changing Root View Controller of a iOS Window
How to Create Percentage of Total Width Using Autolayout
How to Get List of Available Bluetooth Devices
Post Request with a Simple String in Body with Alamofire
Disable Gesture to Pull Down Form/Page Sheet Modal Presentation