You can read it in portuguese 🇧🇷 here
The next challenge is always hanging around in our lives and today there is a cool one to share, printing the full content of a scrollview.
But before we can print anything the content should be transformed into an UIImage. Next is the breakdown of the process:
- Create an UIScrollView extension with a method called toImage that returns an UIImage;
- Use UIGraphicsBeginImageContext with contentSize;
- Store: contentOffset, frame, showsVerticalScrollIndicator and showsVerticalScrollIndicator. So they can be restored to original values before changes;
- Set showsVerticalScrollIndicator and showsHorizontalScrollIndicator to false. Scrolls are not required in the final image;
- Set contentOffset to CGPoint.zero. The frame should not be influentiated by user’s swipes;
- Set scrollview frame to its content values, that way all the content becames “visible”;
- Render scrollview’s layer in the current context;
- Transform current context into an image;
- Reset stored values to its original values, and;
- End the context returning the generated image as the result.
Transforming that into code:
Now, with a image in hands a UIPrintInteractionController can be called to do the rest of the heavy job.
The only import needed is UIKit and following is a function where we receive the scrollview to print and a function to call when failure happens.
The code should be easy to read, it comprises the creation of UIPrintInteractionController with desired settings and passing the image from toImage to printingItem.
With that apps now can print full content of any UIScrollView, or use it as an image. 😉👋