我想从 UITextView 获取多行字符串并将其绘制在图像上, 图像中文本的位置必须与 UITextView 中的位置完全相同,但我不想将 textView 添加到 UIImageView,我需要一个由这些文本组成的新图像,这可能吗?有什么建议吗?
最佳答案
您可以将任何 UIView 的内容捕获到 UIImage 中。首先,创建一个空的 UIView 并将 UIImageView 和 UITextView 定位为 subview :
// Assumes you have UIImageView *myImageView and UITextField *myTextField
UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero];
[parentView addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 100, 100)];
[parentView addSubview:myTextField];
[myTextField setFrame:CGRectMake(20, 20, 80, 20)];
[parentView sizeToFit];
现在创建一个图形上下文并将parentView绘制到其中:
UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您现在拥有一个包含 UIImageView 和 UITextField 内容的 UIImage,可以通过网络显示、保存或发送。
关于iphone - 如何从 UITextView 中的图像上绘制由多行字符串组成的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1400248/