gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:59:43 25 4
gpt4 key购买 nike

在图片上写文字的代码:

-(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com