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