如何将文本转换为图像?

Laz*_*rus 25 uiimage ios

我正在尝试编写一个函数来将文本字段文本转换为图像(示例).我试图搜索一个例子,大多数样本也是在图像上覆盖文本.有可能给我一些例子或提示如何做到这一点?

Rob*_*Rob 52

有几种方法是可行的.

  1. 如果您有现有的UITextField,UITextView或者UILabel您只想呈现为图像,则可以使用传统的快照方法,例如:

    - (UIImage *)imageForView:(UIView *)view
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    
        if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
            [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];  // if we have efficient iOS 7 method, use it ...
        else
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];         // ... otherwise, fall back to tried and true methods
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果您想要一个通用的"从文本创建图像"例程,在iOS 7中,它看起来像:

    - (UIImage *)imageFromString:(NSString *)string attributes:(NSDictionary *)attributes size:(CGSize)size
    {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        [string drawInRect:CGRectMake(0, 0, size.width, size.height) withAttributes:attributes];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    以上将创建一个图像,其大小将根据文本而变化.显然,如果您只想要一个固定大小的图像,那么使用常量frame,而不是动态构建它.

    无论如何,你可以使用上面这样:

    NSString *string = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    
    NSDictionary *attributes = @{NSFontAttributeName            : [UIFont systemFontOfSize:20],
                                 NSForegroundColorAttributeName : [UIColor blueColor],
                                 NSBackgroundColorAttributeName : [UIColor clearColor]};
    
    UIImage *image = [self imageFromString:string attributes:attributes size:self.imageView.bounds.size];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果您需要支持早期的iOS版本,可以使用以下技术:

    - (UIImage *)imageFromString:(NSString *)string font:(UIFont *)font size:(CGSize)size
    {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        [string drawInRect:CGRectMake(0, 0, size.width, size.height) withFont:font lineBreakMode: NSLineBreakByWordWrapping];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    
    Run Code Online (Sandbox Code Playgroud)

每种方法都有很多很多种排列.这取决于你想要达到的目标.


另一种方法是简单地同时拥有UIImageViewUILabel/ UITextView对象的视图,如果你有来自服务器的图像,设置的图像UIImageView和文本,设置text了的UILabel/ UITextView.


Ami*_*sar 9

NSString *string = @"Some text";
UIGraphicsBeginImageContext(CGSizeMake(80, 50));
[string drawAtPoint:CGPointMake(10, 20)
           withFont:[UIFont systemFontOfSize:12]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

你可以从这开始


Sur*_*gch 5

迅速回答

如果你只需要得到的可见内容的图像UIFieldViewUITextViewUILabel则可以使用下面的方法。

func imageFromView(myView: UIView) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(myView.bounds.size, false, UIScreen.mainScreen().scale)
    myView.drawViewHierarchyInRect(myView.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return image
}
Run Code Online (Sandbox Code Playgroud)

特例

当文本内容滚动出框架时,例如在 a 中UITextView,上面的解决方案只会捕获可见区域。为了获得所有内容,一种解决方案是在制作图像之前将文本视图的副本调整为其内容视图的大小。

func imageFromTextView(textView: UITextView) -> UIImage {

    // Make a copy of the textView first so that it can be resized 
    // without affecting the original.
    let textViewCopy = UITextView(frame: textView.frame)
    textViewCopy.attributedText = textView.attributedText
    
    // resize if the contentView is larger than the frame
    if textViewCopy.contentSize.height > textViewCopy.frame.height {
        textViewCopy.frame = CGRect(origin: CGPointZero, size: textViewCopy.contentSize)
    }
    
    // draw the text view to an image
    UIGraphicsBeginImageContextWithOptions(textViewCopy.bounds.size, false, UIScreen.mainScreen().scale)
    textViewCopy.drawViewHierarchyInRect(textViewCopy.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    return image
}
Run Code Online (Sandbox Code Playgroud)

笔记

  • 也可以只调整原始文本视图的大小,然后再次调整它的大小。但是,制作临时副本似乎更简单。
  • 获取视图副本的另一种方法是存档和取消存档
  • 或者您可以直接写入UIImage.