IOS 照片添加水印

在做项目的时候我们需要将拍摄的照片做上标记防止图片被他人盗用,所以这就需要在照片的上面加上水印,以表示此照片的独一无二。
加水印不是要在上面添加上几个Label,而是我们要把字画到图片上成为一个整体。
提供一个方法 此方法只需要传递一个要加水印的图片和水印的内容就达到效果

-(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name

{

    NSString* mark = name;

    int w = img.size.width;

    int h = img.size.height;

    UIGraphicsBeginImageContext(img.size);

    [img drawInRect:CGRectMake(0, 0, w, h)];

    NSDictionary *attr = @{

                           NSFontAttributeName: [UIFont boldSystemFontOfSize:20],   //设置字体

                           NSForegroundColorAttributeName : [UIColor redColor]      //设置字体颜色

                           };

    [mark drawInRect:CGRectMake(0, 10, 80, 32) withAttributes:attr];                 //左上角

    [mark drawInRect:CGRectMake(w - 80, 10, 80, 32) withAttributes:attr];            //右上角

    [mark drawInRect:CGRectMake(w - 80, h - 32 - 10, 80, 32) withAttributes:attr];   //右下角

    [mark drawInRect:CGRectMake(0, h - 32 - 10, 80, 32) withAttributes:attr];        //左下角

    UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return aimg;

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容