iOS 給照片加浮水印,文字

來源:互聯網
上載者:User

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;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.