Am making an iOS application which captures screenshot of a textview filled with some image. The problem is that the screen shot captures the entire screen. How do I take screen shot of UITextView using Swift?
Asked
Active
Viewed 708 times
3 Answers
1
You can save any view as image by using drawViewHierarchyInRect method
UIGraphicsBeginImageContextWithOptions(self.textView.bounds.size, false, 0);
self.textText.drawViewHierarchyInRect(CGRectMake(0, 0, self.textView.frame.size.width, self.textView.frame.size.height),afterScreenUpdates:true)
var screenShot:UIImage = UIGraphicsGetImageFromCurrentImageContext();

Zell B.
- 10,266
- 3
- 40
- 49
-
i want screenshot of all data available in textview means when i enter multiple line then scroll appear than it take screenshot only those portion that display on screen , i want all data screenshot without extending height of textview – Feb 06 '15 at 12:13
-
@ScientistGetAns Unfortunately there is no direct way to achieve what you are looking for. A possible workaround : 1) before taking screenShot extend textView height so it will show all the lines 2) take screenShot 3) set textView height to its original height. With this workaround user will not even see height change so it could be a good workaround – Zell B. Feb 06 '15 at 12:23
-
done, can you tell me how to get number of line exists in UITextView using swift – Feb 07 '15 at 06:21
1
The following worked for me
UIGraphicsBeginImageContextWithOptions(self.textField.bounds.size, false, 0);
self.textField.drawViewHierarchyInRect(self.textField.bounds, afterScreenUpdates: true)
let copied = UIGraphicsGetImageFromCurrentImageContext();
imageView.image = copied
UIGraphicsEndImageContext();

Abubakr Dar
- 4,078
- 4
- 22
- 28
-
thanks it helps. can you tell me how to get number of line exists in UITextView using swift – Feb 07 '15 at 06:21
-
Maybe this link could help you: http://stackoverflow.com/questions/5837348/counting-the-number-of-lines-in-a-textview-lines-wrapped-by-frame-size – Abubakr Dar Feb 07 '15 at 06:55
0
All answers are good but missing is scale
which is necessary
in context
.
Use UIView's drawViewHierarchyInRect
mehtod to capture screen.
//Use screen scale for better images
let screenScale = UIScreen.mainScreen().scale;
//create context
UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, false, screenScale);
//use view for drawing
yourView.drawViewHierarchyInRect(yourView.bounds, afterScreenUpdates: true)
//get captured image
let capturedImage = UIGraphicsGetImageFromCurrentImageContext();
//last dump context
UIGraphicsEndImageContext();
Another sloution : use UIView's layer's renderInContext
//Use screen scale for better images
let screenScale = UIScreen.mainScreen().scale;
//create context
UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, false, screenScale);
//use view for drawing
yourView.layer.renderInContext(UIGraphicsGetCurrentContext())
//get captured image
let capturedImage = UIGraphicsGetImageFromCurrentImageContext();
//last dump context
UIGraphicsEndImageContext();

Paresh Navadiya
- 38,095
- 11
- 81
- 132