Add watermarks and text to photos in iOS

Source: Internet
Author: User

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;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.