溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在iOS中實現(xiàn)一個圖片水印功能

發(fā)布時間:2021-04-07 16:13:09 來源:億速云 閱讀:256 作者:Leah 欄目:移動開發(fā)

怎么在iOS中實現(xiàn)一個圖片水印功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

核心代碼:

將字符串添加到圖形上下文的方法
- (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. 要手動創(chuàng)建一個位圖上下文,創(chuàng)建位圖上下文時,要指定大小,指定的大小,決定著生成圖片的尺寸是多大
void UIGraphicsBeginImageContext(CGSize size);
 
//2. 把內容繪制到上下文當中
//2.1繪制原始圖片
//2.2繪制文字
//2.3繪制logo
 
//3. 從上下文當中生成一張圖片,把上下文當中繪制的所有內容合成在一起生成一張跟上下文尺度一樣的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext() ;
 
//4.手動創(chuàng)建的上下文一定要手動去銷毀掉
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.要手動創(chuàng)建一個位圖上下文
 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.手動創(chuà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.要手動創(chuàng)建一個位圖上下文,創(chuàng)建位圖上下文時,要指定大小,指定的大小,決定著生成圖片的尺寸是多大
  2.把內容繪制到上下文當中
  3.從上下文當中生成一張圖片,把上下文當中繪制的所有內容合成在一起生成一張跟上下文尺度一樣的圖片
  4.手動創(chuàng)建的上下文一定要手動去銷毀掉
  */
}
-(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中實現(xiàn)一個圖片水印功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

ios
AI