Add watermark text on IOS images (watermark text on images on Weibo)

Source: Internet
Author: User

Add watermark text on IOS images (watermark text on Weibo)

 

Create a category (you can go to Baidu if you don't know how to create it ).

Class Object. h file name: UIImage + watermark

 

# Import

 

@ Interface UIImage (watermark)

// Define a method

-(UIImage *) watermarkImage :( NSString *) text;

@ End


. M file implementation

 

# Import "UIImage + watermark. h"

 

@ Implementation UIImage (watermark)

-(UIImage *) watermarkImage :( NSString *) text {

 

// 1. Obtain the context

UIGraphicsBeginImageContext (self. size );

 

// 2. Draw an image

[Self drawInRect: CGRectMake (0, 0, self. size. width, self. size. height)];

 

// 3. Draw watermark text

CGRect rect = CGRectMake (0, self. size. height-20, self. size. width, 20 );

 

NSMutableParagraphStyle * style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

Style. alignment = NSTextAlignmentCenter;

// Text attributes

NSDictionary * dic = @{

NSFontAttributeName: [UIFont systemFontOfSize: 13],

NSParagraphStyleAttributeName: style,

NSForegroundColorAttributeName: [UIColor whiteColor]

};

// Draw the text

[Text drawInRect: rect withAttributes: dic];

 

// 4. Obtain the drawn Image

UIImage * watermarkImage = UIGraphicsGetImageFromCurrentImageContext ();

 

// 5. Draw the ending Image

UIGraphicsEndImageContext ();

 

Return watermarkImage;

}

@ End



Then add a UIImageView import category (UIImage + watermark) file to the viewController view.

 

# Import "ViewController. h"

# Import "UIImage + watermark. h"

@ Interface ViewController ()

 

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

 

 

UIImage * image = [UIImage imageNamed: @ "baymax.jpg"];

 

// Call the objective method implementation function

UIImage * img = [image watermarkImage: @ "@ "];

 

UIImageView * imageView = [[UIImageView alloc] initWithImage: img];

// ImageView. frame = self. view. bounds;

 

[Self. view addSubview: imageView];

}

 

-(Void) didReceiveMemoryWarning {

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

@ End



 

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.