I have a ScrollView with many views inside it. Some views are hidden. When these views are not hidden, the ScrollView expands. If all the views are not hidden, then the ScrollView expands in size and it is larger than the height of the screen. So you will need to scroll to see all of the content.
What I want to do is take a picture of the entire screen with every view not hidden. So, let's say a view is hidden, then I will programmatically unhide the view and then take a picture. I don't want the user to be able to see the screen expand and then collapse though.
This is what I have so far. It works only if the views are already not hidden before I call this function. The problem is that if I try to call this function when the views are hidden, then it only takes a picture of what is currently on screen/visible, it does not take capture everything off-screen/not visible with it.
func createImageFromEntireScreen() -> UIImage {
print("Beginning: \(scrollView.contentSize)")
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0)
print("Middle: \(scrollView.contentSize)")
let savedContentOffset: CGPoint = scrollView.contentOffset
let savedFrame: CGRect = scrollView.frame
let savedIdeaSummaryStackViewHiddenValue = ideaSummaryStackView.hidden.boolValue
let savedAllQuestionsCombinedStackView = allQuestionsCombinedStackView.hidden.boolValue
let savedNotesStackView = notesStackView.hidden.boolValue
// THIS IS THE PROBLEM. If these views were already unhidden
// before the screenshot, then its OK. But if I hide them in
// here then it only takes a picture of what is on screen.
// The reason I unhide it here is so the user doesn't see this happening.
ideaSummaryStackView.hidden = false
ideaSummaryStackView.alpha = 1.0
allQuestionsCombinedStackView.hidden = false
allQuestionsCombinedStackView.alpha = 1.0
notesStackView.hidden = false
notesStackView.alpha = 1.0
scrollView.contentOffset = CGPointZero
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
print("Ending: \(scrollView.contentSize)")
scrollView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let createdImage = UIGraphicsGetImageFromCurrentImageContext()
// ----- Revert to original screen: Begin
ideaSummaryStackView.hidden = savedIdeaSummaryStackViewHiddenValue
allQuestionsCombinedStackView.hidden = savedAllQuestionsCombinedStackView
notesStackView.hidden = savedNotesStackView
scrollView.contentOffset = savedContentOffset
scrollView.frame = savedFrame
// ----- Revert to original screen: End
UIGraphicsEndImageContext()
return createdImage
}
What do you guys think??
UPDATE TO QUESTION:
After some testing, I realize that the contentSize
does not change even after I unhide the views. I updated the question with print("Beginning: \(scrollView.contentSize)")
and others to prove it. However contentSize
does update, if I use delay
functions to give it sometime. It seems that this function is executing too fast for the contentSize
to update when I set hidden = false
. Any ideas how to solve this?