你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 給照片加水印,文字

iOS 給照片加水印,文字

編輯:IOS開發綜合

 

+ (instancetype)waterImageWithText:(UIImage *)img textLogoColor:(NSString *)text1{
/////注:此為後來更改,用於顯示中文。zyq,2013-5-8
CGSize size = CGSizeMake(img.size.width, img.size.height); //設置上下文(畫布)大小
UIGraphicsBeginImageContext(size); //創建一個基於位圖的上下文(context),並將其設置為當前上下文
CGContextRef contextRef = UIGraphicsGetCurrentContext(); //獲取當前上下文
CGContextTranslateCTM(contextRef, 0, img.size.height); //畫布的高度
CGContextScaleCTM(contextRef, 1.0, -1.0); //畫布翻轉
CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), [img CGImage]); //在上下文種畫當前圖片
[[UIColor whiteColor] set]; //上下文種的文字屬性
CGContextTranslateCTM(contextRef, 0, img.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
UIFont *font = [UIFont boldSystemFontOfSize:30];
//[text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font]; //此處設置文字顯示的位置
//[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withFont:font];
UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext(); //從當前上下文種獲取圖片
UIGraphicsEndImageContext(); //移除棧頂的基於當前位圖的圖形上下文。
return targetimg;

}

+ (instancetype)waterImageWithImage:(UIImage *)bgImage logo:(NSString *)logo{

UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);// 1.創建一個基於位圖的上下文

[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];// 2.畫背景

UIImage *waterImage = [UIImage imageNamed:logo];// 3.畫左下角的水印

CGFloat scale = 1;

CGFloat margin = 5;

CGFloat waterW = waterImage.size.width * scale;

CGFloat waterH = waterImage.size.height * scale;

CGFloat waterX = 0;

CGFloat waterY = bgImage.size.height - waterH - margin;

[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];


UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();//4.從上下文中取得制作完畢的UIImage對象

UIGraphicsEndImageContext();// 5.結束上下文、

return newImage;
}
+ (instancetype)waterImageWithText:(UIImage *)img textLogo:(NSString *)text1{

CGSize size=CGSizeMake(img.size.width, img.size.height);

UIGraphicsBeginImageContext(size);

CGContextRef contextRef=UIGraphicsGetCurrentContext();//上下文

//轉換矩陣

CGContextTranslateCTM(contextRef, 0, img.size.height);//畫布的高度.移動函數

CGContextScaleCTM(contextRef, 1.0, -1.0);//縮放函數

CGContextDrawImage(contextRef,CGRectMake(0,0,img.size.width,img.size.height),[img CGImage]);//在上下文種畫當前圖片

[[UIColor redColor] set]; //上下文種的文字屬性

CGContextTranslateCTM(contextRef, 0, img.size.height);

CGContextScaleCTM(contextRef, 1.0, -1.0);

UIFont *font = [UIFont boldSystemFontOfSize:30];

[text1 drawInRect:CGRectMake(120, img.size.height-40, img.size.width-80, 40) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];

UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();//從當前上下文種獲取圖片

UIGraphicsEndImageContext();

return targetimg;
}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved