take image from UITextView

依然范特西╮ 提交于 2019-12-12 04:36:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!