Preface:
The function of adding watermark to images in many commonly used software is very common, such as Weibo, WeChat, Toutiao and other pictures.
First, let’s understand what watermarks are and their functions?
Watermark: Translucent logos, texts, and icons added to the picture to prevent others from stealing pictures
The function of watermark: tells you where this image comes from, mainly added by some websites for copyright issues and advertisements.
Related knowledge points: Quartz2D related content
Core code:
Methods to add strings to graph context - (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs - (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs Methods to add strings to graph context - (void)drawAtPoint:(CGPoint)point; // mode = kCGBlendModeNormal, alpha = 1.0 - (void)drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha; - (void)drawInRect:(CGRect)rect; // mode = kCGBlendModeNormal, alpha = 1.0 - (void)drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
Basic steps:
//1. To manually create a bitmap context, when creating a bitmap context, you need to specify the size and the specified size determines how big the size of the generated image is.void UIGraphicsBeginImageContext(CGSize size); //2. Draw the content into the context//2.1 Draw the original picture//2.2 Draw text//2.3 Draw the logo //3. Generate an image from the context, and combine all the content drawn in the context to generate an image with the same context size as the contextUIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ; //4. The manually created context must be destroyed manuallyUIGraphicsEndImageContext() ;
Encapsulated example code:
#import <UIKit/> NS_ASSUME_NONNULL_BEGIN @interface SWWaterMarkImage : UIImage -(UIImage *)WaterImageWithImage:(UIImage *)image ImageLogo:(UIImage *)imageLogo title:(NSString *)string ; +(UIImage *)WaterImageWithImage:(UIImage *)image ImageLogo:(UIImage *)imageLogo title:(NSString *)string ; @end NS_ASSUME_NONNULL_END
@implementation SWWaterMarkImage -(UIImage *)WaterImageWithImage:(UIImage *)image ImageLogo:(UIImage *)imageLogo title:(NSString *)string { //1. To manually create a bitmap context UIGraphicsBeginImageContext() ; //2. Draw into content context //Original image rendering [image drawInRect:CGRectMake(0, 0, , )]; //Word NSDictionary *attributeDict = @{ NSFontAttributeName : [UIFont systemFontOfSize:], NSForegroundColorAttributeName:[UIColor whiteColor], // NSBackgroundColorAttributeName :[UIColor redColor] } ; CGRect rectSize = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesDeviceMetrics attributes:attributeDict context:nil] ; CGFloat x = - - 10 ; CGFloat y = - 30 ; [string drawAtPoint:CGPointMake(x, y) withAttributes:attributeDict] ; //Logo Picture CGFloat waterW = 30; CGFloat waterH = 30; CGFloat waterX = x - waterW - 10 ; CGFloat waterY = y - 3 ; [imageLogo drawInRect:CGRectMake(waterX, waterY, waterW, waterH)] ; //3. Generate a new picture from the current context UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ; //4. The manually created context must be destroyed manually UIGraphicsEndImageContext() ; return newImage ; } +(UIImage *)WaterImageWithImage:(UIImage *)image ImageLogo:(UIImage *)imageLogo title:(NSString *)string { return [[self alloc]WaterImageWithImage:image ImageLogo:imageLogo title:string] ; } @end
#import "" #import "" @interface ViewController () @property(nonatomic,strong)UIImageView *imageView ; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Step on generating a watermarked image: /* Images can be generated in any method, not necessarily in the drawRect: method 1. To manually create a bitmap context, when creating a bitmap context, you need to specify the size and the specified size determines how big the size of the generated image is. 2. Draw the content into the context 3. Generate a picture from the context, and combine all the content drawn in the context to generate a picture with the same scale as the context. 4. The context created manually must be destroyed manually */ } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UIImage *newImage = [SWWaterMarkImage WaterImageWithImage:[UIImage imageNamed:@"18d8bc3eb13533fa65021ddba5d3fd1f40345b8b"] ImageLogo:[UIImage imageNamed:@"logo"] title:@"Wuhu Asia Atomic Network Technology Co., Ltd."] ; //5. Display the generated image on the imageView = [[UIImageView alloc]init] ; = CGRectMake(0, 100, 375, 250) ; = newImage ; [ addSubview:] ; } @end
The packaging is very rough. If you have any good suggestions, please leave a message below. Let's communicate together and encourage each other⛽️
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.