温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用iOS实现图片水印与封装

发布时间:2021-09-27 14:09:09 来源:亿速云 阅读:111 作者:小新 栏目:编程语言

这篇文章主要介绍了如何使用iOS实现图片水印与封装,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

首先我们了解一下什么是水印及其作用?

水印:在图片上加的防止他人盗图的半透明logo、文字、图标

水印的作用:告诉你这个图片从哪来的,主要是一些网站为了版权问题、广告而添加的。

核心代码:

将字符串添加到图形上下文的方法- (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 ;@endNS_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]; //生成一张加水印图片步骤: /*  可以在任何方法中生成图片,不一定在drawRect:方法中生成  1.要手动创建一个位图上下文,创建位图上下文时,要指定大小,指定的大小,决定着生成图片的尺寸是多大  2.把内容绘制到上下文当中  3.从上下文当中生成一张图片,把上下文当中绘制的所有内容合成在一起生成一张跟上下文尺度一样的图片  4.手动创建的上下文一定要手动去销毁掉  */}-(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

感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用iOS实现图片水印与封装”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI