iphone - iOS SDK写在UIImage上绘制文本

标签 iphone ios xcode sdk uiimage

在图片上写文字的代码:

-(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代码内部进行报告,以在图像上进行彩色文本处理。

谢谢。

最佳答案

我不确定我是否理解正确,但是要设置文本字段中文本的颜色,您需要将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];
}

关于iphone - iOS SDK写在UIImage上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324984/

相关文章:

swift - (观看连接)WCSession 缺少其委托(delegate)(仅在 XCode11.0 中)

ios - React Native 使用设备上的预捆绑文件,即使是 DEV 模式

objective-c - iOS MVC : where does the model fit-in to a CoreData application?

iphone - 更改NSDate的时间?

iphone - 使用 v1.1 API 获取 iOS Twitter 用户时间线

iphone - UitextView 中的文本被截断?如何正确地逐页滚动文本

ios - 当 transform.rotation.y > M_PI 时,UIView 的图层旋转不会将背景保持在顶部

ios - 获取正确的 ViewController 实例以与委托(delegate)函数一起使用

ios - 快速访问应用程序委托(delegate)变量延迟 View 加载

ios - UIWebView 不要弹跳和滚动