您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
怎么在iOS中实现一个图片水印功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
核心代码:
将字符串添加到图形上下文的方法 - (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]; //生成一张加水印图片步骤: /* 可以在任何方法中生成图片,不一定在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中实现一个图片水印功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。