Posted By

magickaito on 04/26/11


Tagged


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

espinallab
Tintenklecks
cnathan
Rakesh


programatically add text on image


 / Published in: Objective C
 

  1. -(UIImage *)addText:(UIImage *)img text:(NSString *)text1
  2. {
  3. //get image width and height
  4. int w = img.size.width;
  5. int h = img.size.height;
  6. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  7. //create a graphic context with CGBitmapContextCreate
  8. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  9. CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  10. CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);
  11. char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
  12. CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);
  13. CGContextSetTextDrawingMode(context, kCGTextFill);
  14. CGContextSetRGBFillColor(context, 255, 0, 0, 1);
  15. CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
  16. CGContextShowTextAtPoint(context, 60, 390, text, strlen(text));
  17. //Create image ref from the context
  18. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  19. CGContextRelease(context);
  20. CGColorSpaceRelease(colorSpace);
  21. return [UIImage imageWithCGImage:imageMasked];
  22.  
  23. }

Report this snippet  

You need to login to post a comment.