Add watermark text on IOS images (watermark text on Weibo) and ios watermark
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 <UIKit/UIKit. h>
@ 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
[SelfdrawInRect: 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 = [[NSMutableParagraphStyledefaultParagraphStyle] mutableCopy];
Style. alignment = NSTextAlignmentCenter;
// Text attributes
NSDictionary * dic = @{
NSFontAttributeName: [UIFontsystemFontOfSize: 13],
NSParagraphStyleAttributeName: style,
NSForegroundColorAttributeName: [UIColorwhiteColor]
};
// Draw the text
[Text drawInRect: rectwithAttributes: 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 {
[SuperviewDidLoad];
UIImage * image = [UIImageimageNamed: @ "baymax.jpg"];
// Call the objective method implementation function
UIImage * img = [image watermarkImage: @ "@ "];
UIImageView * imageView = [[UIImageViewalloc] initWithImage: img];
// ImageView. frame = self. view. bounds;
[Self. viewaddSubview: imageView];
}
-(Void) didReceiveMemoryWarning {
[SuperdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@ End