Drawing NSString to UIImage

自闭症网瘾萝莉.ら 提交于 2020-01-30 08:17:05

问题


I have a NSString object. I want to write it to existing UIImage object. UIImage object already has some image associate with it. I want to write string to same image. How do i achieve it?


回答1:


EDIT: Following are basic step to achieve to edit an UIImage and write text on it.

  1. Get CGImage from UIImage.
  2. Render CGImage On context.
  3. Render Text you want to write on the same context.
  4. Get Bitmap of context and make CGImage out of that.
  5. Get UIImage from CGImage....

It would be much easier to show text on and label and show that on image view. And answer for that are already there.

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




回答2:


An NSString cannot become a UIImage. However, it sounds like all you want to do is plunk some text on top of an image, and that can be achieved easily.

  1. Create a UILabel *label. Set the text to your string with label.text = myString;.

  2. Create a UIImageView *imageView. Set the image of the view to your image with imageView.image = myimage.

  3. Add the UILabel as a subview of the UIImageView (as a subclass of UIView, it takes subviews) with [imageView addSubview:label]; (or drop the label on the imageview if you're using the IB).

Make sure to set the background of the label to [UIColor clearColor] or set the alpha = 0.0 so that it's transparent.




回答3:


You would draw (composite) both the image and the string into a graphics context, and then grab the resulting UIImage.

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

UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();



回答4:


Here is my code. I have a 35x480 View and I draw text rotated by 90 degrees on it. I hope this helps. I draw the Image and then the text so it shows up. It looks like a window's window title.

I draw the image then the text on the 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];
}


来源:https://stackoverflow.com/questions/7343814/drawing-nsstring-to-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!