前言:
常用的许多软件中图片加水印的功能是非常常见的,如微博,微信,今日头条等等图片上都会有。
首先我们了解一下什么是水印及其作用?
水印:在图片上加的防止他人盗图的半透明logo、文字、图标
水印的作用:告诉你这个图片从哪来的,主要是一些网站为了版权问题、广告而添加的。
相关知识点:Quartz2D相关内容
核心代码:
将字符串添加到图形上下文的方法
- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs
- (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs
将字符串添加到图形上下文的方法
- (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;
基本步骤:
//1. 要手动创建一个位图上下文,创建位图上下文时,要指定大小,指定的大小,决定着生成图片的尺寸是多大
void UIGraphicsBeginImageContext(CGSize size);
//2. 把内容绘制到上下文当中
//2.1绘制原始图片
//2.2绘制文字
//2.3绘制logo
//3. 从上下文当中生成一张图片,把上下文当中绘制的所有内容合成在一起生成一张跟上下文尺度一样的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ;
//4.手动创建的上下文一定要手动去销毁掉
UIGraphicsEndImageContext() ;
封装的实例代码:
SWWaterMarkImage.h
#import <UIKit/UIKit.h>
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
SWWaterMarkImage.m
@implementation SWWaterMarkImage
-(UIImage *)WaterImageWithImage:(UIImage *)image ImageLogo:(UIImage *)imageLogo title:(NSString *)string {
//1.要手动创建一个位图上下文
UIGraphicsBeginImageContext(image.size) ;
//2.绘制到内容上下文中
//原始图片渲染
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//文字
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图片
CGFloat waterW = 30;
CGFloat waterH = 30;
CGFloat waterX = x - waterW - 10 ;
CGFloat waterY = y - 3 ;
[imageLogo drawInRect:CGRectMake(waterX, waterY, waterW, waterH)] ;
//3.从当前的上下文当中生成一张新的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ;
//4.手动创建的上下文一定要手动去销毁掉
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];
//生成一张加水印图片步骤:
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UIImage *newImage = [SWWaterMarkImage WaterImageWithImage:[UIImage imageNamed:@"18d8bc3eb13533fa65021ddba5d3fd1f40345b8b"] ImageLogo:[UIImage imageNamed:@"logo"] title:@"芜湖亚原子网络科技有限公司"] ;
//5.将生成的image显示到imageView上去
self.imageView = [[UIImageView alloc]init] ;
self.imageView.frame = CGRectMake(0, 100, 375, 250) ;
self.imageView.image = newImage ;
[self.view addSubview:self.imageView] ;
}
@end
封装的很糙,如果有好的建议欢迎大家在下方留言,我们一起交流一下,共勉⛽️
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程网的支持。