0

I'm developing an iOS application where a user can add as many texts and images as possible (just like typing and adding emojis on the iPhone textview) to a UITextView.

Is there any way to take a snapshot to cover the scrolled items. I need to post whatever the user has added into the UITextView to his/her Facebook wall. At the moment, the snapshot only captures what is visible on the screen. I'm using Swift.

The Code:

UIGraphicsBeginImageContextWithOptions(message.contentSize, false, 0);
message.layer.renderInContext(UIGraphicsGetCurrentContext());
let img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Michael Woyo
  • 138
  • 2
  • 15

1 Answers1

0

When you capture your snapshot, use (0, 0) as the origin and the scroll view's contentSize as the size instead of using your scroll view's bounds directly. This is because UIScrollView (and its subclasses, like UITextView) shifts its bounds when scrolling, so the bounds rectangle represents only the portion of the scroll view that is visible.

Edit: it looks like UITextView might not draw the parts of itself that are outside its bounds, so you may have to temporarily adjust its frame, as per this answer.

Community
  • 1
  • 1
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • Thanks @Zev Eisenberg, I did that but I realized that the image size is that of the content but the scrolled items were lost. Below is the code UIGraphicsBeginImageContextWithOptions(message.contentSize, false, 0); message.layer.renderInContext(UIGraphicsGetCurrentContext()), afterScreenUpdates: true); let img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); – Michael Woyo Feb 07 '15 at 05:51
  • Sorry about the formatting. I'm trying to figure out how it works around here. – Michael Woyo Feb 07 '15 at 05:53
  • Best would be to edit your original post and add the code there. – Zev Eisenberg Feb 07 '15 at 05:55
  • Alright, thanks @Zev Eisenberg. This helps but unfortunately, I can't vote up because of limited privileges. – Michael Woyo Feb 07 '15 at 06:05