programatically add text on image


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.