0

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?

JEL
  • 1,540
  • 4
  • 23
  • 51
  • He I think your are looking just like this one **http://stackoverflow.com/questions/3539717/getting-a-screenshot-of-a-uiscrollview-including-offscreen-parts** – Muzahid Jan 23 '16 at 05:21
  • Your question has nothing to do with Xcode or Objective-C. Please don't add unnecessary tags. – rmaddy Jan 23 '16 at 05:24
  • @Md.MuzahidulIslam It looks similar but I don't think it will work because they are not unhiding something at the moment you have to take a picture. The scrollView contentSize gets bigger when you unhide the views and my picture does not take this into account. What do you think? – JEL Jan 23 '16 at 05:26
  • @rmaddy Ok, will add new tags. Can you help with this problem though? – JEL Jan 23 '16 at 05:27
  • @Md.MuzahidulIslam I tried the solution but no good. It still only takes a picture of the visible portion. Can you suggest something else please? – JEL Jan 23 '16 at 05:35
  • @Md.MuzahidulIslam Please see my updated question. It has comments in my code which explain where I think the main problem is. The comments in the code starts with "THIS IS THE PROBLEM" – JEL Jan 23 '16 at 05:49

1 Answers1

0

I have face this problem in objective C, so I have solve like this

  1. When you want to take screen shot then Set the scrollview size equal to its content size.
  2. Then You have to take the screen shot of scrollview.
  3. After capturing screen shot , set the scrollview's size as before.
  4. You will have to finish this process so fast. So user can not see what actually happen.

If you want to see the code i can share but it is in Objective C.

Note: This process can't capture those part which are scrollable in scrollview.

Nikhil Modi
  • 187
  • 2
  • 7
  • Yes, please send the code! I would love to see it -- thank you for replying. – JEL Jan 23 '16 at 19:42
  • Plz see this Link for show the code. https://www.dropbox.com/s/ab15367x354tuw7/screenShot.rtf?dl=0 if any query then inform me. – Nikhil Modi Feb 04 '16 at 08:57