ios - 在 Objective-C 中以编程方式将文本置于图像上

标签 ios objective-c

我想以编程方式在图像上添加文本,但我似乎找不到如何操作的方法。我找到了 one解决方案在这里,但是很多东西都被弃用了,所以它不起作用......

请帮忙!

编辑:

这是我的代码:

UIImage *backgroundImage = image;

NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];

[stringAttributes setObject: [UIFont fontWithName:@"Helvetica" size:20] forKey: NSFontAttributeName];
[stringAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];
[stringAttributes setObject: [NSNumber numberWithFloat: 2.0] forKey: NSStrokeWidthAttributeName];
[stringAttributes setObject: [UIColor blackColor] forKey: NSStrokeColorAttributeName];

[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];

NSString *myString = [NSString stringWithFormat:@"Yolo"];

[myString drawInRect:CGRectMake(0, 0, 200, 50) withAttributes:stringAttributes];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = result;

新编辑:

我想澄清一些事情以便更好地理解这个问题。我的应用程序允许用户通过短信或电子邮件发送他们自己拍摄的照片,我想在照片上添加一些来自字符串的预写文本。

所以我的问题是:如何将字符串中的文本添加到照片上?

最佳答案

这花了很长时间,但我想通了。

代码:

NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];

[stringAttributes setObject: [UIFont fontWithName:@"Avenir Book" size:250] forKey: NSFontAttributeName];
[stringAttributes setObject: [UIColor whiteColor] forKey: NSForegroundColorAttributeName];

NSString *placeString = [NSString stringWithFormat:@"Text here."];

CGSize size = [placeString sizeWithAttributes:stringAttributes];
//Create a bitmap context into which the text will be rendered.
UIGraphicsBeginImageContext(size);
//Render the text.
[placeString drawAtPoint:CGPointMake(0.0, 0.0) withAttributes:stringAttributes];
//Retrieve the image.
UIImage *imagene = UIGraphicsGetImageFromCurrentImageContext();

UIImage *mergedImage = _imageView.image;

CGSize newSize = image.size;
UIGraphicsBeginImageContext(newSize);

//Use existing opacity as is.
[mergedImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

//Apply supplied opacity if applicable.
CGRect drawingRect = (CGRect) {.size = size};
drawingRect.origin.x = (newSize.width - size.width) * 0.01;
drawingRect.origin.y = (newSize.height - size.height) * 0.03;
[imagene drawInRect:drawingRect blendMode:kCGBlendModeNormal alpha:1];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[_imageView setImage:newImage];
self.imageView.image = newImage;

关于ios - 在 Objective-C 中以编程方式将文本置于图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33137416/

相关文章:

IOS > 使用 POST 在 Safari 中打开 URL

ios - 如何在 iOS 中复制 OpenGL ES 帧缓冲区?

ios - 从 Foursquare URL 加载图像到 UIImageView

objective-c - 我如何将 UIDocumentInteractionController 与 MPMediaPickerController 一起使用

ios - UICustomTableViewCell - 在中间垂直设置标签

c# - 带有图像的 MonoTouch UITableView

iphone - 根据 UITextView 框架大小调整文本

ios - 使用 Firebase 累积角标(Badge)编号的任何方法?

iOS - UIButton 操作的选择器中的硬编码值

iphone - iPhone 上的基本 HTTP 身份验证