Drawing NSString for UIImage

I have an NSString object. I want to write it to an existing UIImage object. The UIImage object already has some image associated with it. I want to write a string for the same image. How do I achieve this?

+1
source share
4 answers

EDIT: The following are the basic steps to achieve editing UIImage and writing text to it.

  • Get CGImage from UIImage.
  • Render CGImage In Context.
  • The render text you want to write in the same context.
  • Get the context bitmap and make a CGImage from this.
  • Get UIImage from CGImage ....

It would be much easier to show the text and the sticker and show it in the image view. And the answer is already there.

Then update it to the file or show it on the screen ....

+2
source

An NSString cannot become a UIImage . However, it seems that all you want to do is add text on top of the image, and this can be easily achieved.

  • Create a UILabel *label . Set the text to a string using label.text = myString; .

  • Create a UIImageView *imageView . Set the image image to your image using imageView.image = myimage .

  • Add UILabel as a subset of UIImageView (as a subclass of UIView , it will take subviews) using [imageView addSubview:label]; (or remove the mark on the image if you are using IB).

Be sure to set the background of the label to [UIColor clearColor] or set alpha = 0.0 to make it transparent.

+1
source

You will draw (compose) the image and line in a graphic context, and then take the resulting UIImage.

To draw, use renderInContext . To draw text, use CGContextShowTextAtPoint . To get the resulting image

 UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext(); 
+1
source

Here is my code. I have a 35x480 view and I draw text rotated 90 degrees. Hope this helps. I draw an image and then text so that it appears. It looks like a window title.

I draw an image, then text in drawRect.

 @synthesize topView; @synthesize delegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; topView.backgroundColor = [UIColor clearColor]; topView.opaque = NO; } return self; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { UIGraphicsBeginImageContext(img.image.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); // draw titleborder CGRect titleRect = CGRectMake(0, 0, 35, 480); NSString *filePath = [[NSBundle mainBundle] pathForResource:@"titleborder" ofType:@"png"]; UIImage *titleImage = [[UIImage alloc] initWithContentsOfFile:filePath]; [titleImage drawInRect:titleRect]; [self bringSubviewToFront:topView]; UIFont *font = [UIFont boldSystemFontOfSize:16.0]; CGRect textRect = CGRectMake(0, rect.size.height/2, 480, 40); NSLog(@"text rect frame: %@", NSStringFromCGRect(textRect)); [self drawText:[[NSString alloc] initWithFormat:@"My Window Title"] rect:textRect context:context font:font red:1.0 green:1.0 blue:1.0 alpha:1.0] ; CGContextRestoreGState(context); } - (void) drawText: (NSString *)text rect: (CGRect)rect context:(CGContextRef)context font:(UIFont *)font red:(CGFloat)r green: (CGFloat)g blue:(CGFloat)b alpha:(CGFloat)alpha { CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetRGBFillColor(context, r, g, b, alpha); // 6 CGContextSetRGBStrokeColor(context, 1, 1, 1, 1); CGAffineTransform transform1 = CGAffineTransformMakeRotation(-90.0 * M_PI/180.0); CGContextConcatCTM(context, transform1); CGSize sizeOfString = [text sizeWithFont:font]; CGContextTranslateCTM(context, (sizeOfString.width/2) - 420,-232); [text drawInRect:rect withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft]; } 
0
source

All Articles