简体   繁体   English

UIimageview加文本叠加保存为新图像?

[英]UIimageview plus Text overlay saved as NEW image?

I know there are more than enough resources on how have a text box overlay on a portion of an image view. 我知道在文本视图的一部分上如何覆盖文本框方面有足够的资源。 However, is there any way to save the image + overlaid text as a new image upon upload? 但是,有什么方法可以在上传图片时将图片+覆盖的文字另存为新图片吗?

  1. User chooses photo and image view populates with image 用户选择照片,然后在视图中填充图像
  2. User enters text on top of the image view 用户在图像视图上方输入文本
  3. User hits "upload" button, and image+text is rendered as a new image that incorporates text on top of it before saving to back-end database. 用户单击“上载”按钮,然后将图像+文本呈现为新图像,并在其上合并文本,然后再保存到后端数据库。

Is this possible? 这可能吗? How? 怎么样?

You can do this using Core Graphics/Quartz 2D. 您可以使用Core Graphics / Quartz 2D执行此操作。 I'm not too experienced with Core Graphics myself, but check out the documentation here that describes loading a graphic context with an image, which you can then save out to a file after modifying, or just drawing an image from scratch and saving it out: 我本人对Core Graphics不太熟悉,但是请查看此处的文档,该文档描述了如何使用图像加载图形上下文,然后可以在修改后将其保存到文件中,或者仅从头开始绘制图像并将其保存出来。 :

Link 1: Drawing/Printing for iOS 链接1: iOS版绘图/打印

Link 2: Quartz 2D Programming Guide 链接2: Quartz 2D编程指南

Quick example of drawing text from the Quartz 2D Programming Guide under the "Text" section: Quartz 2D编程指南中“文本”部分下的绘图文本快速示例:

void MyDrawText (CGContextRef myContext, CGRect contextRect) // 1
{
  float w, h;
  w = contextRect.size.width;
  h = contextRect.size.height;

  CGAffineTransform myTextTransform; // 2
  CGContextSelectFont (myContext, // 3
                "Helvetica-Bold",
                 h/10,
                 kCGEncodingMacRoman);
  CGContextSetCharacterSpacing (myContext, 10); // 4
  CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5

  CGContextSetRGBFillColor (myContext, 0, 1, 0, .5); // 6
  CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1); // 7
  myTextTransform =  CGAffineTransformMakeRotation  (MyRadians (45)); // 8
  CGContextSetTextMatrix (myContext, myTextTransform); // 9
  CGContextShowTextAtPoint (myContext, 40, 0, "Quartz 2D", 9); // 10
}

Here's what the code does: 代码是这样的:

  1. Takes as parameters a graphics context and a rectangle to draw to. 将图形上下文和要绘制到的矩形作为参数。
  2. Declares storage for the affine transform. 声明用于仿射变换的存储。
  3. Sets the font to Helvetica and the font size to the height of the page rectangle divided by 10. The font size is in text space units. 将字体设置为Helvetica,将字体大小设置为页面矩形的高度除以10。字体大小以文本空间为单位。 In this example, the text is drawn to a resizable window. 在此的示例文本绘制到可调整大小的窗口。 When the user resizes the window, the text resizes as well. 当用户调整窗口大小时,文本也将调整大小。 The encoding is set to kCGEncodingMacRoman, but the only other choice is kCGEncodingFontSpecific. 编码设置为kCGEncodingMacRoman,但唯一的其他选择是kCGEncodingFontSpecific。
  4. Sets the character spacing to 10 text space units. 将字符间距设置为10个文本空间单位。 You call this function only if you want to add the additional space to the advance between the origin of one glyph and the origin of the next glyph. 仅当您要在一个字形的原点和下一个字形的原点之间的行距中添加额外的空间时,才调用此函数。
  5. Sets the text drawing mode to fill and stroke. 将文本绘制模式设置为填充和描边。
  6. Sets the fill color to green with an alpha value of .5 for a partially transparent fill. 将填充颜色设置为绿色,alpha值为0.5,以表示部分透明的填充。 Note that this is not a text-specific attribute. 请注意,这不是特定于文本的属性。 The fill color applies to the graphics state. 填充颜​​色适用于图形状态。
  7. Sets the stroke color to opaque blue. 将笔触颜色设置为不透明的蓝色。 This is another attribute that is not text specific. 这是另一个非特定于文本的属性。
  8. Creates an affine transform that performs a 45 degree rotation. 创建执行45度旋转的仿射变换。 The MyRadians routine is an application-defined convenience function for computing degrees from radians. MyRadians例程是应用程序定义的便捷函数,用于根据弧度计算度数。 You either need to supply your own routine to convert degrees to radians, or you need to substitute this call with a value that specifies radians. 您或者需要提供自己的例程以将度数转换为弧度,或者需要用指定弧度的值代替此调用。 Otherwise, this code example will not compile. 否则,此代码示例将无法编译。
  9. Sets the text matrix to the transform created in the last step. 将文本矩阵设置为在上一步中创建的转换。
  10. Draws the text, passing the x and y coordinates in text space to start the drawing at (40,0), an array of characters to draw, and a value that specifies the length of the text array. 绘制文本,在文本空间中传递x和y坐标以开始绘制(40,0),绘制一个字符数组,以及一个指定文本数组长度的值。 In this case, you pass a C-style string and the value 9 to specify the number of characters. 在这种情况下,您传递C风格的字符串和值9以指定字符数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM