iOS實(shí)現(xiàn)圖片水印與簡(jiǎn)單封裝示例代碼
前言:
常用的許多軟件中圖片加水印的功能是非常常見(jiàn)的,如微博,微信,今日頭條等等圖片上都會(huì)有。
首先我們了解一下什么是水印及其作用?
水?。涸趫D片上加的防止他人盜圖的半透明logo、文字、圖標(biāo)
水印的作用:告訴你這個(gè)圖片從哪來(lái)的,主要是一些網(wǎng)站為了版權(quán)問(wèn)題、廣告而添加的。
相關(guān)知識(shí)點(diǎn):Quartz2D相關(guān)內(nèi)容
核心代碼:
將字符串添加到圖形上下文的方法 - (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. 要手動(dòng)創(chuàng)建一個(gè)位圖上下文,創(chuàng)建位圖上下文時(shí),要指定大小,指定的大小,決定著生成圖片的尺寸是多大 void UIGraphicsBeginImageContext(CGSize size); //2. 把內(nèi)容繪制到上下文當(dāng)中 //2.1繪制原始圖片 //2.2繪制文字 //2.3繪制logo //3. 從上下文當(dāng)中生成一張圖片,把上下文當(dāng)中繪制的所有內(nèi)容合成在一起生成一張跟上下文尺度一樣的圖片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ; //4.手動(dòng)創(chuàng)建的上下文一定要手動(dòng)去銷毀掉 UIGraphicsEndImageContext() ;
封裝的實(shí)例代碼:
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.要手動(dòng)創(chuàng)建一個(gè)位圖上下文 UIGraphicsBeginImageContext(image.size) ; //2.繪制到內(nèi)容上下文中 //原始圖片渲染 [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.從當(dāng)前的上下文當(dāng)中生成一張新的圖片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ; //4.手動(dòng)創(chuàng)建的上下文一定要手動(dòng)去銷毀掉 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.要手動(dòng)創(chuàng)建一個(gè)位圖上下文,創(chuàng)建位圖上下文時(shí),要指定大小,指定的大小,決定著生成圖片的尺寸是多大 2.把內(nèi)容繪制到上下文當(dāng)中 3.從上下文當(dāng)中生成一張圖片,把上下文當(dāng)中繪制的所有內(nèi)容合成在一起生成一張跟上下文尺度一樣的圖片 4.手動(dòng)創(chuàng)建的上下文一定要手動(dòng)去銷毀掉 */ } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UIImage *newImage = [SWWaterMarkImage WaterImageWithImage:[UIImage imageNamed:@"18d8bc3eb13533fa65021ddba5d3fd1f40345b8b"] ImageLogo:[UIImage imageNamed:@"logo"] title:@"蕪湖亞原子網(wǎng)絡(luò)科技有限公司"] ; //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
封裝的很糙,如果有好的建議歡迎大家在下方留言,我們一起交流一下,共勉⛽️
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS開(kāi)發(fā)的UI制作中動(dòng)態(tài)和靜態(tài)單元格的基本使用教程
這篇文章主要介紹了iOS開(kāi)發(fā)的UI制作中動(dòng)態(tài)和靜態(tài)單元格的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12IOS實(shí)現(xiàn)百度地圖自定義大頭針和氣泡樣式
這篇文章主要介紹了 IOS百度地圖自定義大頭針和氣泡的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12IOS開(kāi)發(fā)之@property的詳細(xì)介紹
這篇文章主要介紹了IOS開(kāi)發(fā)之@property的詳細(xì)介紹的相關(guān)資料,希望通過(guò)本文能幫助到大家,大家理解并會(huì)使用,需要的朋友可以參考下2017-09-09IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動(dòng)畫(huà),感興趣的小伙伴們可以參考一下2016-03-03