如何从 UITextView 中的图像上绘制由多行字符串组成的文本?

发布于 2024-08-04 11:04:53 字数 135 浏览 2 评论 0原文

我想从 UITextView 获取多行字符串并将其绘制在图像上, 图像中文本的位置必须与 UITextView 中的位置完全相同,但我不想将 textView 添加到 UIImageView,我需要一个由这些文本组成的新图像,这可能吗?有什么建议这样做吗?

i want to get a multiple line string from a UITextView and draw it on a image,
the position of text in the image must exactly same as the position in UITextView but i dont want jus add the textView to UIImageView, i need a new image consists of those text, is it possible? any suggestion to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停顿的约定 2024-08-11 11:04:53

您可以将任何 UIView 的内容捕获到 UIImage 中。首先,创建一个空的 UIView 并将 UIImageView 和 UITextView 定位为子视图:

// 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();

现在您有了一个 UIImage,其中包含 UIImageView 和 UITextField 的内容,可以通过网络显示、保存或发送。

You can capture the contents of any UIView into a UIImage. First, create an empty UIView and position your UIImageView and UITextView as subviews:

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

Now create a graphics context and draw parentView into it:

UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

You now have a UIImage with the contents of your UIImageView and UITextField to display, save, or send over the network.

烛影斜 2024-08-11 11:04:53

这是一个很好的解决方案,谢谢。

我想补充一点,您需要添加

#import <QuartzCore/QuartzCore.h>

到您的导入中,这样警告就会消失。

This was a good solution, thanks.

I would like to add that you need to add

#import <QuartzCore/QuartzCore.h>

To your imports so the warnings go away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文