如何在图像上写文字并保存

Posted

技术标签:

【中文标题】如何在图像上写文字并保存【英文标题】:how can i write a text on the image and save it 【发布时间】:2014-01-06 23:16:32 【问题描述】:

我尝试先上传一张图片,然后在上面写一段文字,然后将这两个项目保存为一张图片。 我是目标 c 的新手,所以我的代码可能不是实现目标的最佳方式,任何帮助和建议都会对我有所帮助。 谢谢你们 这是我的代码:

这个编辑器不允许我把我的方法声明放在上面 在此输入代码

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *data = UIImagePNGRepresentation(image);

NSString *mijnImage =@"mijnImage.png";

NSArray *route = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

NSString *imageRoute = [route objectAtIndex:0];

NSString *routeNaarDeBestaand = [imageRoute stringByAppendingPathComponent:mijnImage];

[data writeToFile:routeNaarDeBestaand atomically:YES];

[[self imageView]setImage:image];

[self dismissViewControllerAnimated:YES completion:Nil];

self.imageView.image = [self drawText:@"Test String" inImage:image atPoint:CGPointMake(10, 20)];

这是另一个方法声明同样的问题,编辑器不允许我把它放在上面

UIFont *font = [UIFont boldSystemFontOfSize:100];

UIGraphicsBeginImageContext(self.view.frame.size);

[originalImage drawInRect:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];

CGRect rect = CGRectMake(point.x, point.y,lblText.frame.size.width, lblText.frame.size.height);

rect.size = [text sizeWithAttributes:@NSFontAttributeName:[UIFont systemFontOfSize:150.0f]];

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];

[attributes setObject:font forKey:NSFontAttributeName];

[attributes setObject:[NSNumber numberWithFloat:4] forKey:NSStrokeWidthAttributeName];

[attributes setObject:[UIColor whiteColor] forKey:NSStrokeColorAttributeName];

[text drawInRect:rect withAttributes:attributes];
[attributes removeObjectForKey:NSStrokeWidthAttributeName];
[attributes removeObjectForKey:NSStrokeColorAttributeName];
[attributes setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
[text drawInRect:rect withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

【问题讨论】:

您可以使用可移动标签使其动态化。在文本字段中获取用户输入,将文本分配给标签,并允许用户使用 UITouch 或手势将标签移动到图像视图上的任何位置 【参考方案1】:

我真的不知道是什么问题,但我这样做了.. 让我知道它是否适合你

- (void)viewDidLoad

    UIImage *image    = [UIImage imageNamed:@"image"];
    UIImage *newImage = [self image:image withText:@"Hello World" atPoint:CGPointMake(20, 20)];

    [self saveImage:image    withName:@"original"];
    [self saveImage:newImage withName:@"new"];


- (UIImage*)image:(UIImage*)image withText:(NSString*)text atPoint:(CGPoint)point

    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [image drawInRect:rect];

    NSDictionary *attributes = @NSFontAttributeName           : [UIFont boldSystemFontOfSize:20],
                                 NSStrokeWidthAttributeName    : @(4),
                                 NSStrokeColorAttributeName    : [UIColor whiteColor];
    [text drawAtPoint:point withAttributes:attributes];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;


- (void)saveImage:(UIImage*)image withName:(NSString*)name

    NSString  *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString  *imagePath = [documents stringByAppendingPathComponent:name];

    [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];

【讨论】:

【参考方案2】:

试试这个

  +(UIImage*) drawText:(NSString*) text 
         inImage:(UIImage*)  image 
         atPoint:(CGPoint)   point 
 

    UIFont *font = [UIFont boldSystemFontOfSize:12];
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [[UIColor whiteColor] set];
    [text drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
 

【讨论】:

【参考方案3】:

这个答案将使文本在 UIImage 的中心垂直和水平对齐。 注意:不是我的答案,它是从几个答案中收集的

- (UIImage*)image:(UIImage*)image withText:(NSString*)text
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[image drawInRect:rect];

/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
/// Set text horizontal alignment
paragraphStyle.alignment = NSTextAlignmentCenter;

NSDictionary *attributes = @NSFontAttributeName           : [UIFont boldSystemFontOfSize:16],
                             NSStrokeWidthAttributeName    : @(4),
                             NSStrokeColorAttributeName    : [UIColor whiteColor],
                             NSParagraphStyleAttributeName : paragraphStyle;

CGSize size = [text sizeWithAttributes:attributes];

CGRect newRect = CGRectMake(rect.origin.x,
                      rect.origin.y + (rect.size.height - size.height)/2.0,
                      rect.size.width,
                      size.height);

[text drawInRect:newRect withAttributes:attributes];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

【讨论】:

以上是关于如何在图像上写文字并保存的主要内容,如果未能解决你的问题,请参考以下文章

一本用开源思维在github上写的NLP书籍

如何从图像文字数组中选择单个图像并将其保存为swift 5中的用户默认值

在背景图像上写文字

在球面SceneKit上写文字?

我们如何在 EditText 边框上写文字? [复制]

如何在flutter中使用rootBundle加载图片?