2011-09-08 80 views
1

我有一个NSString对象。 我想将它写入现有的UIImage对象。 UIImage对象已经有一些图像与它关联。 我想写字符串到相同的图像。 我如何实现它?将NSString绘制为UIImage

回答

1

NSString不能成为UIImage。但是,听起来你想要做的只是在图像上叠加一些文本,而且这很容易实现。

  1. 创建一个UILabel *label。使用label.text = myString;将文本设置为您的字符串。

  2. 创建一个UIImageView *imageView。使用imageView.image = myimage将视图图像设置为图像。

  3. 添加UILabelUIImageView的子视图(作为UIView一个子类,它需要子视图)与[imageView addSubview:label];(或滴在imageview的标签,如果你正在使用的IB)。

确保将标签的背景设置为[UIColor clearColor]或设置alpha = 0.0以便它是透明的。

2

编辑: 以下是实现编辑UIImage和写入文本的基本步骤。

  1. 从UIImage获取CGImage。
  2. 呈现CGImage上下文。
  3. 渲染要在同一上下文中编写的文本。
  4. 获取上下文的位图并使CGImage不在此范围内。
  5. 获取的UIImage从CGImage ....

这将是更容易显示在文本和标签,并显示在图像视图。 答案已经存在。

然后更新到文件或显示在屏幕上....

1

你会画(复合)两者的图像和字符串转换成图形上下文,然后抢造成的UIImage。

绘制图像使用renderInContext。 要使用文字CGContextShowTextAtPoint。 为了得到最终的图像

UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext(); 
0

这是我的代码。我有一个35x480视图,并绘制旋转90度的文字。我希望这有帮助。我绘制图像,然后绘制文字,使其显示出来。它看起来像一个窗口的窗口标题。

我绘制图像,然后绘制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]; 
}