如何将文本转换为图像?

我想写一个函数来将文本字段文本转换为图像( 示例 )。 我试图寻找一个例子,大部分的样本也覆盖了图片上的文字。 有没有可能给我一些例子或提示如何做到这一点?

有几种方法是可能的。

  1. 如果您只有一个现有的UITextFieldUITextViewUILabel ,您可以使用传统的快照方法,例如:

     - (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; } 
  2. 如果你想要一个通用的“从文本创build图像”例程,在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; } 

    以上将创build一个图像,其大小将根据文字而有所不同。 显然,如果你只是想要一个固定大小的图像,然后使用常量frame ,而不是dynamic构build它。

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

     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]; 
  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; } 

这些每个都有许多排列组合。 这只取决于你想要达到的目标。


另一种方法是在视图中简单地同时使用UIImageViewUILabel / UITextView对象,并且如果您有来自服务器的图像,请设置UIImageView的图像和文本,设置UILabel / UITextViewtext

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

你可以从这开始

您可以尝试创build您自己的自定义UIView子类,在其上绘制NSString (您的文本),然后将其转换为UIImage 。 您只能在-drawRect:方法中在UIView上绘制文本。 这是你的子类的一个想法。

 @interface TextView : UIView { NSString *_text; } - (id)initWithFrame:(CGRect)frame andText:(NSString *)text; @end @implementation TextView - (id)initWithFrame:(CGRect)frame andText:(NSString *)text { if (self = [super initWithFrame:frame]) { _text = text; } [self setNeedsDisplay]; // calls the -drawRect method return self; } - (void)drawRect:(CGRect)rect { [_text drawAtPoint:/* location of text*/ withAttributes:/* style of text*/]; } 

有关绘制NSString s的更多信息可以在这里find 。 一旦你有了这个视图上的文字, 使用这种技术将其转换为UIImage

这很简单。 你想要将文本转换为UIImage。 只需draw text view's layer into Image context并转换为UIImage。 代码如下。

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

迅速回答

如果您只需要获取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 } 

特例

当文本内容滚动出框架时,如在UITextView ,上面的解决scheme将只捕获可见区域。 为了得到所有的东西,一个解决scheme就是将文本视图的副本调整为其内容视图的大小,然后再制作其图像。

 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 } 

笔记

  • 也可以调整原始文本视图的大小,然后重新resize。 但是,做一个临时副本似乎比较简单。
  • 另一种获取视图副本的方法是将其归档并取消归档 。
  • 或者你可以直接写入UIImage
 NSString * ImgString = [[[self.dataDict valueForKey:@"newsItems"]objectAtIndex:indexPath.row]valueForKey:@"image"]; NSURL *imageURL = [NSURL URLWithString:ImgString]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *image = [UIImage imageWithData:imageData]; cell.imageLabel.image = image; 

我想呈现在表视图,所以我写这个代码在单元格索引path行。

(dataDict是我的字典,我得到了我所有的数据)。