Skip to content

Instantly share code, notes, and snippets.

@vnavarro
Last active July 30, 2021 03:31
Show Gist options
  • Save vnavarro/3726b6ad62a51c429135e3358fa59f99 to your computer and use it in GitHub Desktop.
Save vnavarro/3726b6ad62a51c429135e3358fa59f99 to your computer and use it in GitHub Desktop.
Convert full content of scrollview as image
import UIKit
extension UIScrollView {
func toImage() -> UIImage? {
UIGraphicsBeginImageContext(contentSize)
let savedContentOffset = contentOffset
let savedFrame = frame
let saveVerticalScroll = showsVerticalScrollIndicator
let saveHorizontalScroll = showsHorizontalScrollIndicator
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
contentOffset = CGPoint.zero
frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
contentOffset = savedContentOffset
frame = savedFrame
showsVerticalScrollIndicator = saveVerticalScroll
showsHorizontalScrollIndicator = saveHorizontalScroll
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment