Add Text to UIImage

前端 未结 1 1455
清歌不尽
清歌不尽 2021-02-06 11:51

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 wh

相关标签:
1条回答
  • 2021-02-06 12:34

    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;
    }
    
    0 讨论(0)
提交回复
热议问题