codebot
codebot

Reputation: 2656

How to write a text on image iOS?

In my app I'm trying to add a text on an image. I've done it using a post in stackoverflow. It's like the following.

+(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
    int w = img.size.width;
    int h = img.size.height;
    //lon = h - lon;
    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];// "05/05/09";
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);


    //rotate text
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));

    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));


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

    return [UIImage imageWithCGImage:imageMasked];
}

When I'm writing a text using this code, the original image's colour is changed like the following,

Original Image

enter image description here

Output with Text

enter image description here

Can anyone explain why is this happening. and how to fix this??

Thanks in Advance!!

Upvotes: 1

Views: 2472

Answers (3)

Magoo
Magoo

Reputation: 2636

EDIT: This can only be run on the main thread! So it's pretty much useless.

+(UIImage *)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Here's a new one using Core Graphics that can be used on any thread

+ (UIImage *)imageFromView:(UIView *)view
{
    size_t width = view.bounds.size.width*2;
    size_t height = view.bounds.size.height*2;

    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*8);
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef imageContext = CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colourSpace);

    CGContextTranslateCTM(imageContext, 0.0, height);
    CGContextScaleCTM(imageContext, 2.0, -2.0);

    [view.layer renderInContext:imageContext];

    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);
    UIImage *image = [[UIImage alloc] initWithCGImage:outputImage];

    CGImageRelease(outputImage);
    CGContextRelease(imageContext);
    free(imageBuffer);

    return image;
}

Upvotes: 1

Coldsteel48
Coldsteel48

Reputation: 3522

I am using this and it works fine for me

- (UIImage*) drawText:(NSString*) text
         inImage:(UIImage*)  image
         atPoint:(CGPoint)   point
{

    UIFont *font = [UIFont fontWithName:FONT_BOLD size:20];

    UIGraphicsBeginImageContext(image.size);

    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);

    [UIColorFromRGB(0x515151) set];

    [text drawInRect:CGRectIntegral(rect) withFont:font];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

Upvotes: 1

Waseem Ahmad
Waseem Ahmad

Reputation: 43

We need to create a image context and apply drawing and get final image from this context. You can try this method. Hope this help you.

+ (UIImage *) addText:(UIImage *)img text:(NSString *)text
{
    CGRect rect = [[UIScreen mainScreen] bounds];

    // create a context according to image size
    UIGraphicsBeginImageContext(rect.size);

    // draw image
    [img drawInRect:rect];


    UIFont* font = [UIFont systemFontOfSize:14.0];

    /// Make a copy of the default paragraph style
    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    /// Set line break mode
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    /// Set text alignment
    paragraphStyle.alignment = NSTextAlignmentCenter;

    NSDictionary *attributes = @{ NSFontAttributeName: font,
                                  NSParagraphStyleAttributeName: paragraphStyle };

    CGRect textRect = CGRectMake(20, 160.0, 280.0, 44);

    /// draw text
    [text drawInRect:textRect withAttributes:attributes];

    // get as image
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Upvotes: 2

Related Questions