2009-10-30 20 views
6

Cómo agregar texto en una imagen y mostrarla como una sola imagen. ¿Estoy seleccionando la imagen de la Biblioteca de fotos y luego escribiendo texto en ella y quiero guardarla como una imagen? ¿Alguien puede saber a quién tratar?Agregar texto a UIImage

Respuesta

6

He estado usando el siguiente código para agregar un texto a UIImage, es de esperar que ayudará

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