How to use iOS to realize Image watermarking and Encapsulation
This article mainly introduces how to use iOS to achieve image watermarking and encapsulation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
First of all, let's understand what a watermark is and what it does.
Watermark: translucent logo, text, icon added to the picture to prevent others from stealing the image.
The function of watermark: tell you where this picture comes from, mainly added by some websites for copyright issues and advertisements.
Core code:
Method for adding strings to graphics context-(void) drawAtPoint: (CGPoint) point withAttributes: (nullable NSDictionary *) attrs- (void) drawInRect: (CGRect) rect withAttributes: (nullable NSDictionary *) attrs method for adding strings to graphics 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, specify the size, which determines the size of the generated picture void UIGraphicsBeginImageContext (CGSize size); / / 2. Draw the content into context / / 2.1 draw the original picture / / 2.2 draw text / / 2.3 draw logo//3. Generate a picture from the context and combine all the content drawn in the context to generate a picture with the same context scale UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); / / 4. The context created manually must destroy the UIGraphicsEndImageContext () manually.
Encapsulated instance code:
SWWaterMarkImage.h
# import 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; @ endNS_ASSUME_NONNULL_END
SWWaterMarkImage.m
Implementation SWWaterMarkImage- (UIImage *) WaterImageWithImage: (UIImage *) image ImageLogo: (UIImage *) imageLogo title: (NSString *) string {/ / 1. To manually create a bitmap context UIGraphicsBeginImageContext (image.size); / / 2. Draw into the content context / / render the original picture [image drawInRect:CGRectMake (0,0, image.size.width, image.size.height)]; / / text NSDictionary * attributeDict = @ {NSFontAttributeName: [UIFont systemFontOfSize:20.f], NSForegroundColorAttributeName: [UIColor whiteColor], / / NSBackgroundColorAttributeName: [UIColor redColor]}; CGRect rectSize = [string boundingRectWithSize:CGSizeMake (MAXFLOAT, 30) options:NSStringDrawingUsesDeviceMetrics attributes:attributeDict context:nil]; CGFloat x = image.size.width-rectSize.size.width-10 CGFloat y = image.size.height-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. Manually created context must manually destroy UIGraphicsEndImageContext (); return newImage;} + (UIImage *) WaterImageWithImage: (UIImage *) image ImageLogo: (UIImage *) imageLogo title: (NSString *) string {return [[self alloc] WaterImageWithImage:image ImageLogo:imageLogo title:string];} @ end
ViewController.m
# import "ViewController.h" # import "SWWaterMarkImage.h" @ interface ViewController () @ property (nonatomic,strong) UIImageView * imageView; @ end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad]; / / generate a watermarked image step: / * you can generate an image in any method, not necessarily in the drawRect: method. To manually create a bitmap context, when creating a bitmap context, specify the size, which determines the size of the generated picture. 2. Draw the content into the context 3. Generate a picture from the context and combine all the content drawn in the context to produce a picture with the same context scale. Manually created context must be manually destroyed * /}-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {UIImage * newImage = [SWWaterMarkImage WaterImageWithImage: [UIImage imageNamed:@ "18d8bc3eb13533fa65021ddba5d3fd1f40345b8b"] ImageLogo: [UIImage imageNamed:@ "logo"] title:@ "Wuhu Sub-Atomic Network Technology Co., Ltd."]; / / 5. Display the generated image on imageView self.imageView = [UIImageView alloc] init]; self.imageView.frame = CGRectMake (0100,375,250); self.imageView.image = newImage; [self.view addSubview:self.imageView];} @ end
Thank you for reading this article carefully. I hope the article "how to use iOS to achieve picture watermarking and encapsulation" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!