2011-09-08 34 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]; 
}