将文本添加到UIImage

将文本添加到UIImage

本文介绍了将文本添加到UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本添加到图像中并显示为一个图像。
我从照片库中选择图像,然后将文字写入其中并希望保存为一张图像?那么有人知道应该对谁进行处理吗?

How to Add Text into an Image and display as one Image. Iam selecting image from Photo Library and then writing text onto it and want to save as one image?so can anyone know who to deal with it?

推荐答案

我一直在使用以下代码向UIImage添加文本,希望它会有所帮助

I have been using the following code to add a text to UIImage, hopefully it will help

UIImage *img = [self drawText:@"Your String"
                                inImage:[UIImage imageNamed:@"empty_image.png"]
                                atPoint:CGPointMake(15, 25)];



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

    NSMutableAttributedString *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text]];

    // text color
    [textStyle addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, textStyle.length)];

    // text font
    [textStyle addAttribute:NSFontAttributeName  value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, textStyle.length)];

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

    // add text onto the image
    [textStyle drawInRect:CGRectIntegral(rect)];

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

    return newImage;
}

这篇关于将文本添加到UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:48