Draw text and add to UIImage iOS 5/6

I have a text box where I want the text to be added to UIImage.

How can I draw an NSString for UIImage?

I searched and found many examples, but it does not work. Xcode just gives me a lot of errors.

Simply put, I want to draw an NSString for UIimage. The UIImage must be the same size as the predefined UIImageView and be centered.

Any ideas?

+5
source share
3 answers

I think the solution is here .. I used this method in one of my applications here lbltext is the object of my shortcut. this method creates a new image with the given text and returns the object of the new image with text.

-(UIImage *) drawText:(NSString*) text inImage:(UIImage*)image atPoint:(CGPoint)point 
{
    UIFont *font = lblText.font;
    UIGraphicsBeginImageContext(iv.frame.size);
    [image drawInRect:CGRectMake(0,0,iv.frame.size.width,iv.frame.size.height)];
    CGRect rect = CGRectMake(point.x,point.y,lblText.frame.size.width, lblText.frame.size.height);
    [lblText.textColor set];
    [text drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

iv.center .. ,

CGPoint point;
point.x=30; // dynamicaly just for example lblText.frame.origin.x;
point.y=40; //lblText.frame.origin.y;

...

UIImage *img = [self drawText:@"Test String" inImage:originalImage atPoint:point];

, img - UIImage ... img imager .

...

[newimageviewObj setImage:img];

,

+6

UIImage UIView, . NSString UIView. - , UIView.

, :

UIImageView - UIImage.

UILabel - text NSString.

UILabel UIImageView.

, , UIImageView .

+1

, . , :

  • aUIImageView aView;
  • aUITextView aView;
  • ScreenShot aView;

.

, , . , 1 2, UIGraphics. ( , , .)

PS: , CGImage CGImage. , .

0

All Articles