0

I am implementing print functionality in my application for which I want to send a picture of the screen to the printer SDK.

I am creating a dummy view on which I have a Scroll View and on this Scrollview I have a list a list of products to print added as a subview. The height of this scroll view is larger than the screen size of the iPad as the list is quite big.

I want to capture pictures of the whole Scroll View with the whole list (the list with products not visible and out or UIScreen). Also, I don't want this view to be visible to the user. Everything should happen in the background.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Possibly Related: https://stackoverflow.com/questions/3539717/getting-a-screenshot-of-a-uiscrollview-including-offscreen-parts. – Ahmad F Feb 27 '19 at 12:19

1 Answers1

0

You can render the view in scrollview and get a UIImage of that.

In the code below the view viewInScrollView is subview in UIScrollView and all elements are present in that view.

UIGraphicsBeginImageContext(scrollView.contentSize)      
viewInScrollView.layer.renderInContext(UIGraphicsGetCurrentContext())
var myImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

the myImage variable will have the image of whole scrollView.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Talha Ahmad Khan
  • 3,416
  • 5
  • 23
  • 38
  • shouldn't you make the size the same size as the contentView as the contents could be larger than the view size – Scriptable Feb 27 '19 at 12:16
  • sorry updating the code, forgot to change that part. Thanks – Talha Ahmad Khan Feb 27 '19 at 12:18
  • How to do this without presenting the view to the user? If I do hide the view the crash on this UIGraphicsGetCurrentContext occurs. – akshay tule Feb 27 '19 at 12:20
  • 1
    @akshaytule here is a related issue, https://stackoverflow.com/questions/11061850/capture-the-non-active-uiview-as-an-uiimage , I hope it will answer your question. In the accepted solution the guy said you can remove from superview instead of hiding. – Talha Ahmad Khan Feb 27 '19 at 12:25