前言:
常用的許多軟件中圖片加水印的功能是非常常見的,如微博,微信,今日頭條等等圖片上都會有。
首先我們了解一下什么是水印及其作用?
水?。涸趫D片上加的防止他人盜圖的半透明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]; //生成一張加水印圖片步驟: /* 可以在任何方法中生成圖片,不一定在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
封裝的很糙,如果有好的建議歡迎大家在下方留言,我們一起交流一下,共勉⛽️
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答