Xcode - 图像编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode - Image editing
提问by Lloydworth
I'm developing an image editing application on the iPad. It is like a function where you can write text into an image and then save it.
我正在 iPad 上开发一个图像编辑应用程序。它就像一个功能,您可以将文本写入图像然后保存。
The user will be able to select an image from the iPad's photo gallery. When a photo is selected, it will navigate to another view showing an ImageView, TextField, 'Test' button and 'Confirm' button.
用户将能够从 iPad 的照片库中选择图像。选择照片后,它将导航到另一个视图,显示 ImageView、TextField、“测试”按钮和“确认”按钮。
- The ImageView will display the selected photo.
- The TextField enables the user to enter some text.
- When the Test Button is tapped, the text entered will appear on the image.
- When the Confirm Button is tapped, the image (with the text on it) will be saved.
- ImageView 将显示选定的照片。
- TextField 使用户能够输入一些文本。
- 当点击测试按钮时,输入的文本将出现在图像上。
- 当点击确认按钮时,图像(带有文本)将被保存。
I know how to code the part that allows the user to select an image from the photo gallery and navigating to another view showing the selected image on the ImageView.
我知道如何编码允许用户从照片库中选择图像并导航到在 ImageView 上显示所选图像的另一个视图的部分。
So i need help on how to display the text on the image and saving the image (with the text on it).
所以我需要关于如何在图像上显示文本和保存图像(上面有文本)的帮助。
回答by logancautrell
There are a couple of ways. The easiest it so just take a snapshot of the super view using this code.
有几种方法。最简单的方法是使用此代码拍摄超级视图的快照。
CGRect rect = CGRectMake(<your values>);
UIGraphicsBeginImageContext(rect.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This is a duplicate of How to convert UIView as UIImage?.
回答by Prashant Bhayani
add a label in your UIImageView object (which contains a image from photo gallery).set frame of this label when ever you want text on image. you can add it by this:
在您的 UIImageView 对象中添加一个标签(其中包含来自照片库的图像)。当您需要图像上的文本时,设置此标签的框架。您可以通过以下方式添加它:
[yourUiimageView addSubview: yourLabel];
then crop your image by this:
然后通过以下方式裁剪您的图像:
UIGraphicsBeginImageContext(yourUiimageView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[yourUiimageView.layer renderInContext:ctx];
UIImage *albumThumbImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(albumThumbImage);
Now save this data to document directory. this image will contains the image with text.
现在将此数据保存到文档目录。此图像将包含带有文本的图像。