问题
I have a UITextView containing text only and I want to take a snapshot from it. I created it programmatically.
UITextView *textView = [[UITextView alloc] init];
If the background color of the UITextView is black and the text color is white, I want to parse each pixel to know if I'm on the white or black pixel.
How to do the snapshot to get an image from the UITextView (only if possible) ?
回答1:
implement this method as UIView category, and you would can draw any UIView as image:
- (UIImage*)drawAsImage
{
// setup context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f); // use same scale factor as device
CGContextRef c = UIGraphicsGetCurrentContext();
// render view
[self.layer renderInContext:c];
// get reslting image
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
回答2:
You can use below method for taking screenshot.
-(UIImage *) screenshot
{
CGRect rect;
rect=yourTextView.Frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
回答3:
Yes it is possible. You can use this method for take the snapshot of the textview.
- (UIImage*)screenShotOfView:(UITextView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And use this code to call the method
UITextView *textView = [[UITextView alloc] init];
[self screenShotOfView:textView];
来源:https://stackoverflow.com/questions/26970535/take-image-from-uitextview