My function can add watermark text to an image rotated 45 degrees and 90 degrees
+(UIImage *)drawText:(NSString *)text diagonallyOnImage:(UIImage *)image rotation:(WatermarkRotation)rotation{ UIColor *textColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:0.2];//[UIColor colorWithWhite:0.5 alpha:1.0]; UIFont *font = [UIFont systemFontOfSize:250]; // Compute rect to draw the text inside NSDictionary *attr = @{NSForegroundColorAttributeName: textColor, NSFontAttributeName: font}; CGSize textSize = [text sizeWithAttributes:attr]; CGSize imageSize = image.size; // Create a bitmap context into which the text will be rendered. UIGraphicsBeginImageContext(textSize); // Render the text [text drawAtPoint:CGPointMake(0,0) withAttributes:attr]; // Retrieve the image UIImage* img = UIGraphicsGetImageFromCurrentImageContext(); CGImageRef imageRef = [img CGImage]; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); CGContextRef bitmap = CGBitmapContextCreate(NULL, textSize.width, textSize.width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); switch (rotation) { case WatermarkRotation90left: CGContextRotateCTM (bitmap, DEGREES_RADIANS(-90)); CGContextTranslateCTM(bitmap, -textSize.width, 0); break; case WatermarkRotation90right: CGContextRotateCTM (bitmap, DEGREES_RADIANS(90)); CGContextTranslateCTM(bitmap, 0, -textSize.width); break; case WatermarkRotation45ltr: CGContextRotateCTM (bitmap, DEGREES_RADIANS(45)); CGContextTranslateCTM(bitmap, textSize.width/4, -textSize.width/2); break; case WatermarkRotation45rtl: CGContextRotateCTM (bitmap, DEGREES_RADIANS(-45)); CGContextTranslateCTM(bitmap, -textSize.width/2, textSize.width/4); break; default: break; } CGContextDrawImage(bitmap, CGRectMake(0, (textSize.width/2)-(textSize.height/2), textSize.width, textSize.height), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage* newImage = [UIImage imageWithCGImage:ref]; UIGraphicsBeginImageContext( imageSize ); // Use existing opacity as is [image drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)]; if (rotation == WatermarkRotation90left) { [newImage drawInRect:CGRectMake(-((textSize.width/2)-(textSize.height/2)),(imageSize.height/2)-(textSize.width/2),textSize.width,textSize.width) blendMode:kCGBlendModeNormal alpha:1.0]; }else if(rotation == WatermarkRotation90right){ [newImage drawInRect:CGRectMake((imageSize.width-textSize.width/2)-(textSize.height/2),(imageSize.height/2)-(textSize.width/2),textSize.width,textSize.width) blendMode:kCGBlendModeNormal alpha:1.0]; }else{ [newImage drawInRect:CGRectMake((imageSize.width/2)-(textSize.width/2),(imageSize.height/2)-(textSize.width/2),textSize.width,textSize.width) blendMode:kCGBlendModeNormal alpha:1.0]; } UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return mergedImage; }
Enumeration for rotation:
typedef enum:NSUInteger{ WatermarkRotation90left=1, WatermarkRotation90right, WatermarkRotation45ltr, WatermarkRotation45rtl }WatermarkRotation;
Note: use 0 to draw a watermark in the center of the image. (The default case for the switch statement)
Add this macro for a degree in radians:
#define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI)
Hope this helps !!!