iOS SDK Написать на UIImage рисовать текст - PullRequest
2 голосов
/ 30 ноября 2011

Код для написания текста на картинке:

-(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];
}

Это работает хорошо, после того как я создаю ColorPicker для myTextField, теперь я могу выбрать цвет моего 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, чтобы сделать цветной текст на изображении.

Thx.

1 Ответ

0 голосов
/ 20 сентября 2012

Я не уверен, правильно ли я вас понял, но чтобы установить цвет текста из текстового поля, вам нужно перенести ваш textField в метод addText и затем преобразовать UIColor свойства textField.textColor в формат 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];
}
...