我有一個NSString對象。 我想將它寫入現有的UIImage對象。 UIImage對象已經有一些圖像與它關聯。 我想寫字符串到相同的圖像。 我如何實現它?將NSString繪製爲UIImage
1
A
回答
1
NSString
不能成爲UIImage
。但是,聽起來你想要做的只是在圖像上疊加一些文本,而且這很容易實現。
創建一個
UILabel *label
。使用label.text = myString;
將文本設置爲您的字符串。創建一個
UIImageView *imageView
。使用imageView.image = myimage
將視圖圖像設置爲圖像。添加
UILabel
爲UIImageView
的子視圖(作爲UIView
一個子類,它需要子視圖)與[imageView addSubview:label];
(或滴在imageview的標籤,如果你正在使用的IB)。
確保將標籤的背景設置爲[UIColor clearColor]
或設置alpha = 0.0
以便它是透明的。
2
編輯: 以下是實現編輯UIImage和寫入文本的基本步驟。
- 從UIImage獲取CGImage。
- 呈現CGImage上下文。
- 渲染要在同一上下文中編寫的文本。
- 獲取上下文的位圖並使CGImage不在此範圍內。
- 獲取的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];
}
相關問題
- 1. iOS:在UIImage上繪製NSString和邊框
- 2. 將NSString轉換爲UIImage
- 3. 將NSData [UIImage]轉換爲NSString
- 4. 將UIImage繪製到CurrentContext中
- 5. 將NSString繪製到CALayer中
- 6. NSString到UIImage
- 7. 如何將UIImage「繪製」到視圖中?
- 8. 將NSString繪製到PDF頁面
- 9. 如何繪製動畫UIImage?
- 10. 圍繞UIImage繪製邊框
- 11. 在CGMutablePath中繪製UIImage
- 12. 用Core Graphics繪製UIImage?
- 13. 在UIScrollView中繪製UIImage
- 14. 將UIImage繪製爲一個不帶屏外渲染的圓形
- 15. 在CGRect中繪製NSString
- 16. 轉換的NSString NSAttribtuedString繪製
- 17. 如何在iOS中繪製/繪製UIIMage作爲不帶UIImageView的繪畫視圖
- 18. 用UIImageView繪製NSString繪圖字符串
- 19. 將UIImage轉換爲NSString - 不使用編碼和解碼
- 20. 如何使用NSString繪圖功能從文本創建UIImage
- 21. UIImage> NSData> NSString失敗
- 22. 從NSString創建一個UIImage
- 23. 將繪製的視圖轉換爲UIImage並將其添加到UIImageView中iphone
- 24. 如何使用NSString繪圖功能從文本創建UIImage並將此圖像與另一個UIImage結合
- 25. UIImage在UIView上繪製時反轉
- 26. 不能在UIView裏面繪製UIImage
- 27. 刪除在UIImage上繪製的UIBezirePath線
- 28. 繪製圓圈並存儲在UIImage中
- 29. 使用Core Data中的CGRect繪製UIImage
- 30. Ios如何在EAGLContext中繪製UIImage?