Add watermarks and text to photos in iOS
+ (Instancetype) waterImageWithText :( UIImage *) img textLogoColor :( NSString *) text1 {
///// Note: This is changed later and used to display Chinese characters. Zyq
CGSize size = CGSizeMake (img. size. width, img. size. height); // set the context (canvas) size
UIGraphicsBeginImageContext (size); // create a bitmap-based context and set it to the current context
CGContextRef contextRef = UIGraphicsGetCurrentContext (); // obtain the current context
CGContextTranslateCTM (contextRef, 0, img. size. height); // canvas height
CGContextScaleCTM (contextRef, 1.0,-1.0); // flip the canvas
CGContextDrawImage (contextRef, CGRectMake (0, 0, img. size. width, img. size. height), [img CGImage]); // draw the current image in the context
[[UIColor whiteColor] set]; // context text attribute
CGContextTranslateCTM (contextRef, 0, img. size. height );
CGContextScaleCTM (contextRef, 1.0,-1.0 );
UIFont * font = [UIFont boldSystemFontOfSize: 30];
// [Text1 drawInRect: CGRectMake (0, 0,200, 80) withFont: font]; // set the text display position here
// [Text1 drawInRect: CGRectMake (120, img. size. height-40, img. size. width-80, 40) withAttributes: [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, nil];
[Text1 drawInRect: CGRectMake (120, img. size. height-40, img. size. width-80, 40) withFont: font];
UIImage * targetimg = UIGraphicsGetImageFromCurrentImageContext (); // obtain the image from the current context
UIGraphicsEndImageContext (); // remove the graph context based on the current bitmap at the top of the stack.
Return targetimg;
}
+ (Instancetype) waterImageWithImage :( UIImage *) bgImage logo :( NSString *) logo {
Uigraphicsbeginimagecontextwitexceptions (bgImage. size, NO, 0.0); // 1. Create a bitmap-based context
[BgImage drawInRect: CGRectMake (0, 0, bgImage. size. width, bgImage. size. height)]; // 2. Draw the background
UIImage * waterImage = [UIImage imageNamed: logo]; // 3. Draw the watermark in the lower left corner.
CGFloat scale = 1;
CGFloat margin = 5;
CGFloat waterW = waterImage. size. width * scale;
CGFloat waterH = waterImage. size. height * scale;
CGFloat waterX = 0;
CGFloat waterY = bgImage. size. height-waterH-margin;
[WaterImage drawInRect: CGRectMake (waterX, waterY, waterW, waterH)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // 4. Obtain the created UIImage object from the context
UIGraphicsEndImageContext (); // 5. End context,
Return newImage;
}
+ (Instancetype) waterImageWithText :( UIImage *) img textLogo :( NSString *) text1 {
CGSize size = CGSizeMake (img. size. width, img. size. height );
UIGraphicsBeginImageContext (size );
CGContextRef contextRef = UIGraphicsGetCurrentContext (); // Context
// Conversion matrix
CGContextTranslateCTM (contextRef, 0, img. size. height); // canvas height. Move Function
CGContextScaleCTM (contextRef, 1.0,-1.0); // scaling function
CGContextDrawImage (contextRef, CGRectMake (, img. size. width, img. size. height), [img CGImage]); // draw the current image in the context
[[UIColor redColor] set]; // text attribute of the context
CGContextTranslateCTM (contextRef, 0, img. size. height );
CGContextScaleCTM (contextRef, 1.0,-1.0 );
UIFont * font = [UIFont boldSystemFontOfSize: 30];
[Text1 drawInRect: CGRectMake (120, img. size. height-40, img. size. width-80, 40) withAttributes: [NSDictionary attributes: font, NSFontAttributeName, nil];
UIImage * targetimg = UIGraphicsGetImageFromCurrentImageContext (); // obtain the image from the current context
UIGraphicsEndImageContext ();
Return targetimg;
}