Last active
July 30, 2021 03:31
-
-
Save vnavarro/3726b6ad62a51c429135e3358fa59f99 to your computer and use it in GitHub Desktop.
Convert full content of scrollview as image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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