免费开源的iOS开发学习平台

UIImage与绘图:2-添加水印

在类似新浪微博的应用中,上传的图片都会自动添加水印watermark,水印的添加也是通过绘图的方式实现的,即首先在画布上画上图片,然后再添加上水印签名。

添加水印的实现思路

对UIImage对象的修改和加工,经常会使用到UIGraphics类中提供的函数。对UIImage对象进行修改,通常会采取如下步骤:

  • 调用UIGraphicsBeginImageContext方法,开启一个空白的图像上下文;
UIKIT_EXTERN void     UIGraphicsBeginImageContext(CGSize size);
UIKIT_EXTERN void     UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
  • 绘制该上下文中的内容,例如添加图片,添加文字等;
  • 根据最新的图像上下文中的内容,调用UIGraphicsGetImageFromCurrentImageContext函数,得到一个图像合成后的UIImage对象;
UIKIT_EXTERN UIImage* __null_unspecified UIGraphicsGetImageFromCurrentImageContext(void);
  • 调用UIGraphicsEndImageContext函数,关闭上下文,以节省内存。
UIKIT_EXTERN void     UIGraphicsEndImageContext(void); 

示例代码

在控制器类中,我们可以添加如下的代码来为一个UIImage对象添加水印,该方法传入两个参数:

  • image:需要添加水印的图片;
  • string:需要添加的文字

水印的位置添加在左上角,如果有需要可以修改水印文字的位置。

-(UIImage *)addWatermarkInImage:(UIImage *) image WithText:(NSString *) string {
    //开启一个图形上下文
    UIGraphicsBeginImageContext(image.size);
    //绘制上下文:1-绘制图片
    [image drawAtPoint:CGPointMake(0, 20)];
    //绘制上下文:2-添加文字到上下文
    NSDictionary *dict = @{
                           NSFontAttributeName:[UIFont systemFontOfSize:12.0],
                           NSForegroundColorAttributeName:[UIColor blackColor]
                           };
    [string drawAtPoint:CGPointMake(0, 20)
         withAttributes:dict];
    //从图形上下文中获取合成的图片
    UIImage * watermarkImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return watermarkImage;
}

在控制器类中,可以添加一张图片,通过调用addWatermarkInImage:WithText:方法,把添加水印后的合成图片显示出来,代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed:@"logo"];
    NSString *string = @"九九学院";
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, 200, 200);
    //添加水印
    imageView.image = [self addWatermarkInImage:image WithText:string];
    //添加到主视图
    [self.view addSubview:imageView];
    
}

运行结果:

代码完善

在实际的开发过程中,对于水印的添加,由于返回的是一个已经添加过水印的UIImage对象,因此,也可以增加一个UIImage类的分类(Category)。

  • 新增一个UIImage类的分类Category,命名为:MYImage

  • UIImage+MYImage.h文件中添加如下类方法的声明;

#import <UIKit/UIKit.h>
 
@interface UIImage (MYImage)
/*添加水印方法*/
+ (UIImage *)imageWithWaterMarkTitle:(NSString *)string;
@end
  • UIImage+MYImage.m文件中实现方法功能;
#import "UIImage+MYImage.h"
 
@implementation UIImage (MYImage)
+ (UIImage *)imageWithWaterMarkTitle:(NSString *)string{
    //开启上下文,注意:生成的图文上下文的size
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    //添加图片到上下文
    [self drawAtPoint:CGPointMake(0, 20)];
    //添加文字到上下文
    NSDictionary *dict = @{
                           NSFontAttributeName:[UIFont systemFontOfSize:12.0],
                           NSForegroundColorAttributeName:[UIColor blackColor]
                           };
    [string drawAtPoint:CGPointMake(0, 20)
         withAttributes:dict];
    //生成新图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文,节省内存
    UIGraphicsEndImageContext();
    return newImage; 
}
@end

https://github.com/99ios/14.2.2