1.首先创建一个Image类,.h文件复制
/**
* 打水印
*
* @param bg 背景图片
* @param logo 右下角的水印图片
*/
+ (UIImage *)waterImageWithBg:(NSString *)bg logo:(NSString *)logo;
2.在.m文件中实现方法;
+ (UIImage *)waterImageWithBg:(NSString *)bg logo:(NSString *)logo { UIImage *bgImage = [UIImage imageNamed:bg]; // 1.创建一个基于位图的上下文(开启一个基于位图的上下文) UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0); // 2.画背景 [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)]; // 3.画右下角的水印 UIImage *waterImage = [UIImage imageNamed:logo]; CGFloat scale = 0.2; CGFloat margin = 5; CGFloat waterW = waterImage.size.width * scale; CGFloat waterH = waterImage.size.height * scale; CGFloat waterX = bgImage.size.width - waterW - margin; CGFloat waterY = bgImage.size.height - waterH - margin; [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)]; // 4.从上下文中取得制作完毕的UIImage对象 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 5.结束上下文 UIGraphicsEndImageContext(); return newImage; }
然后在要调用的controller中调用此方法
UIImage *newImage = [MJUiimage waterImageWithBg:@"scene" logo:@"logo"]; // 2.显示图片 self.iconView.image = newImage;