2012-10-16 35 views

回答

0
-(UIImage *)imageFromText:(NSString *)text 
{ 
    // set the font type and size 
    UIFont *font = [UIFont systemFontOfSize:20.0]; 
    CGSize size = [text sizeWithFont:font]; 

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
    if (UIGraphicsBeginImageContextWithOptions != NULL) 
     UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    else 
     // iOS is < 4.0 
     UIGraphicsBeginImageContext(size); 

    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
    // 
    // CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]); 

    // draw in context, you can use also drawInRect:withFont: 
    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *Image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return Image; 
} 

在这里,我得到了将文本更改为图像的想法。

+0

现在我想将这个图像与另一个UIimage结合。我该怎么做? – Ann

0

看看UIGraphicsBeginImageContext和相关函数,这可以让你创建一个位图图像和绘图。你可以绘制你的文本和任何你想创建一个新的位图的东西。

相关问题