How to convert text to Image on iOS?

前端 未结 7 2259
庸人自扰
庸人自扰 2020-12-07 18:10

I am trying to write a function to convert the textfield text to an image (example). I have tried to search for an example, most of the sample also is overwrite text on imag

7条回答
  •  有刺的猬
    2020-12-07 18:32

    It's simple. You want to convert text to UIImage. Just draw text view's layer into Image context and convert into UIImage. code is below.

    + (UIImage *) imageWithView:(UITextView *)view
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return img;
    }
    

提交回复
热议问题