最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)圖片水印與簡(jiǎn)單封裝示例代碼

 更新時(shí)間:2019年01月25日 14:33:07   作者:石, 穩(wěn)  
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)圖片水印與簡(jiǎn)單封裝的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言:

常用的許多軟件中圖片加水印的功能是非常常見(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)文章

最新評(píng)論

长顺县| 麻栗坡县| 襄樊市| 青岛市| 泰来县| 宁晋县| 枞阳县| 磐石市| 海淀区| 连城县| 通榆县| 防城港市| 依兰县| 农安县| 德阳市| 耒阳市| 竹溪县| 鸡西市| 伊春市| 河南省| 北辰区| 诏安县| 平和县| 徐汇区| 兖州市| 永平县| 高尔夫| 韩城市| 镇远县| 三门峡市| 巫溪县| 巴中市| 凤城市| 高雄县| 康定县| 吴江市| 博乐市| 漳平市| 墨玉县| 囊谦县| 句容市|