【发布时间】:2012-01-09 15:10:26
【问题描述】:
在图片上写文字的代码:
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);
char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 255, 255, 255, 2);
CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
并通过此操作进行改进:
- (IBAction)drawImage:(id)sender {
NSString *text1 = myTextField.text;
UIImage *updatedImg = [self addText:myImageView.image text:text1];
[myImageView setImage: updatedImg];
}
效果很好,在我为 myTextField 创建了 ColorPicker 之后,现在我可以选择我的 textField 的颜色了。
如何在上面的第一个代码中使用与myTextField相同的颜色?
//this is the code color for drawing text
CGContextSetRGBFillColor(context, 255, 255, 255, 2);
//how can change this to get the color by myTextField?
任何人都可以帮助我。
谢谢。
//--------------------------------------------- ----------------------------------
你好,我需要解决方案:(
尝试更好地解释:
最后,我将通过myTextField.textColor 选择颜色信息并在UIImage 代码中报告以在图像上添加颜色文本。
谢谢。
【问题讨论】:
-
您能否改写“但我不知道如何通过或获取 UIImage 中的信息”?我不明白这个问题。
-
当然,我会在这些代码部分 CGContextSetRGBFillColor(context, 255, 255, 255, 2); 中获取 myTextField 中的颜色;
-
最后我将使用我的颜色选择器来确定图像上的颜色文本。
-
对不起,我还是不明白你在问什么。
-
我有一个 uiimage 函数可以在图像上写文字吗?
标签: iphone ios xcode sdk uiimage