【作者前言】:13年入圈戏挡,分享些本人工作中遇到的點(diǎn)點(diǎn)滴滴那些事兒余耽,17年剛開始寫博客,高手勿噴技健!以分享交流為主勒葱,歡迎各路豪杰點(diǎn)評(píng)改進(jìn)浪汪!
1.應(yīng)用場(chǎng)景:
2.實(shí)現(xiàn)目標(biāo):
應(yīng)項(xiàng)目要求,有些時(shí)候我們需要對(duì)圖片進(jìn)行打水印凛虽、加Logo的操作死遭,在原始圖片的基礎(chǔ)上重新繪制出有文字水印和Logo之后的圖片
3.代碼說明:
/** 根據(jù)需要打入水印的圖片和文字,重新返回繪制結(jié)束后的新的圖片*/
/** waterImage:打入水印的圖片凯旋,即為 如來 */
/** waterString:打入水印的文字呀潭,即為"致敬經(jīng)典" */
- (UIImage *)yp_getNewImageWithOriginalImage:(UIImage *)originalImage waterImage:(UIImage *)waterImage waterString:(NSString *)waterString {
UIGraphicsBeginImageContext(originalImage.size);
// 原始圖片渲染
[originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
CGFloat waterX = originalImage.size.width - 200;
CGFloat waterY = originalImage.size.height - 200;
CGFloat waterW = 200;
CGFloat waterH = 200;
// 打入的水印圖片 渲染
[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
// 打入的水印的文字渲 染
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary * dic = @{
NSFontAttributeName : [UIFont systemFontOfSize:40],
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : [UIColor redColor]
};
[waterString drawInRect:CGRectMake(50, 50, 200, 50) withAttributes:dic];
UIGraphicsEndPDFContext();
// UIImage
UIImage * imageNew = UIGraphicsGetImageFromCurrentImageContext();
return imageNew;
}
//使用方式
UIImage * originalImage = [UIImage imageNamed:@"wukong"];
UIImage * waterImage = [UIImage imageNamed:@"rulai"];
UIImage *newImage = [self yp_getNewImageWithOriginalImage:originalImage waterImage:waterImage waterString:waterString ]
self.imageView.image = newImage;