Iphone 向UIImage添加文本

Iphone 向UIImage添加文本,iphone,Iphone,如何将文本添加到图像中并显示为一个图像。 我正在从照片库中选择图像,然后将文本写入其中,并希望将其另存为一个图像?因此,有人知道该由谁来处理吗?我一直在使用以下代码向UIImage添加文本,希望它能有所帮助 UIImage *img = [self drawText:@"Your String" inImage:[UIImage imageNamed:@"empty_image.png"]

如何将文本添加到图像中并显示为一个图像。
我正在从照片库中选择图像,然后将文本写入其中,并希望将其另存为一个图像?因此,有人知道该由谁来处理吗?

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

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