【问题标题】:iOS SDK Write on UIImage draw textiOS SDK 在 UIImage 上写入绘制文本
【发布时间】: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


【解决方案1】:

我不确定我是否理解正确,但是要设置文本字段中文本的颜色,您需要将 textField 传输到 addText 方法,然后将 textField.textColor 属性的 UIColor 转换为 RGB 格式.

你需要这样做:

-(UIImage *)addText:(UIImage *)img text:(UITextField *)textField
{
    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);

CGColorRef color = [textField.textColor CGColor];

int numComponents = CGColorGetNumberOfComponents(color);

CGFloat red, green, blue, alpha;

if (numComponents == 4)
{
  const CGFloat *components = CGColorGetComponents(color);
  red = components[0];
  green = components[1];
  blue = components[2];
  alpha = components[3];
}

    CGContextSetRGBFillColor(context, red, green, blue, alpha);

    CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多