This is how I take a screenshot of my view:
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.drawViewHierarchyInRect(view.boun
Oh, actually, just following code (swift 4) can work. That is, create a context, and add the item you want in the snapshot. No need to add view in real screen.
Note: currentView
is the view which snapshot based on, commonly, it will be view of whole screen. And addViews
are the views you want to add.
func takeSnapShot(currentView: UIView , addViews: [UIView], hideViews: [UIView]) -> UIImage {
for hideView in hideViews {
hideView.isHidden = true
}
UIGraphicsBeginImageContextWithOptions(currentView.frame.size, false, 0.0)
currentView.drawHierarchy(in: currentView.bounds, afterScreenUpdates: true)
for addView in addViews{
addView.drawHierarchy(in: addView.frame, afterScreenUpdates: true)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
for hideView in hideViews {
hideView.isHidden = false
}
return image!
}