我正在试图编写一个将文本字段文本转换为图像的函数(示例)。我已经尝试搜索样例,大部分样例也是在图像上覆盖文本。是否可能给我一些示例或提示如何做到这一点?
我正在试图编写一个将文本字段文本转换为图像的函数(示例)。我已经尝试搜索样例,大部分样例也是在图像上覆盖文本。是否可能给我一些示例或提示如何做到这一点?
有几种可能的方法。
如果您已经有一个现有的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;
}
如果您想要一个通用的“从文本创建图像”的例程,在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;
}
以上代码将创建一个大小基于文本的图像。如果你只想要一个固定大小的图像,那么使用常量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];
如果你需要支持早期的 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;
}
这些都有很多变化,具体取决于你想要实现什么。
另一种方法是在视图中同时使用UIImageView
和UILabel
/UITextView
对象,如果你从服务器获得了一张图片,就设置UIImageView
的图像,如果有文本,则设置UILabel
/UITextView
的text
属性。
NSString *string = @"Some text";
UIGraphicsBeginImageContext(CGSizeMake(80, 50));
[string drawAtPoint:CGPointMake(10, 20)
withFont:[UIFont systemFontOfSize:12]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您可以从这里开始
如果您只需要获取UIFieldView
、UITextView
或UILabel
的可见内容图像,则可以使用以下方法。
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
中,上面的解决方案只会捕捉到可见区域。为了获取所有内容,一种解决方案是先将文本视图的副本大小调整为其内容视图的大小,然后再制作其图像。
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
}
注意事项
UIImage
中。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
的更多信息可以在这里找到。 一旦您拥有了带有文本的视图,请使用此技术将其转换为 UIImage
。
+ (UIImage *) imageWithView:(UITextView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (UIImage *)imageFromString:(NSString *)string size:(CGSize)size
{
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18],
NSForegroundColorAttributeName : [UIColor whiteColor],
NSBackgroundColorAttributeName : [UIColor redColor]
};
UIGraphicsBeginImageContext(size);
CGRect txtRect = [string boundingRectWithSize:CGSizeZero
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
[string drawAtPoint:CGPointMake(size.width/2 - txtRect.size.width/2, size.height/2 - txtRect.size.height/2) withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
NSString *string = @"A";
UIImage *image = [self imageFromString:string size:imgView.bounds.size];
[imgView.bounds setImage:image];
[imgView.bounds setBackgroundColor:[UIColor redColor];//Full margin with same color of text image color.
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;