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

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

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)長白免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎ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)建一個(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.手動創(chuà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.要手動創(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.手動創(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)建一個(gè)位圖上下文,創(chuàng)建位圖上下文時(shí),要指定大小,指定的大小,決定著生成圖片的尺寸是多大
  2.把內(nèi)容繪制到上下文當(dāng)中
  3.從上下文當(dāng)中生成一張圖片,把上下文當(dāng)中繪制的所有內(nèi)容合成在一起生成一張跟上下文尺度一樣的圖片
  4.手動創(chuà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

看完上述內(nèi)容,你們掌握怎么在iOS中實(shí)現(xiàn)一個(gè)圖片水印功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前標(biāo)題:怎么在iOS中實(shí)現(xiàn)一個(gè)圖片水印功能
文章源于:http://bm7419.com/article4/geiiie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信公眾號虛擬主機(jī)、網(wǎng)站建設(shè)、商城網(wǎng)站、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營