【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢迎各路豪杰点评改进!
1.应用场景:
2.实现目标:
应项目要求,有些时候我们需要对图片进行打水印、加Logo的操作,在原始图片的基础上重新绘制出有文字水印和Logo之后的图片
3.代码说明:
/** 根据需要打入水印的图片和文字,重新返回绘制结束后的新的图片*/
/** waterImage:打入水印的图片,即为 如来 */
/** waterString:打入水印的文字,即为"致敬经典" */
- (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;