IOS给图片添加水印(两种方式)
2019/7/9 23:28:34
本文主要是介绍IOS给图片添加水印(两种方式),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个Label,而是我们把字画到图片上成为一个整体,下面小编给大家分享IOS给图片添加水印(两种方式)。
提供一个方法,此方法只需要传递一个要加水印的图片和水印的内容就达到效果。
第一种方式:
-(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(, , w, h)]; NSDictionary *attr = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:], //设置字体 NSForegroundColorAttributeName : [UIColor redColor] //设置字体颜色 }; [mark drawInRect:CGRectMake(, , , ) withAttributes:attr]; //左上角 [mark drawInRect:CGRectMake(w - , , , ) withAttributes:attr]; //右上角 [mark drawInRect:CGRectMake(w - , h - - , , ) withAttributes:attr]; //右下角 [mark drawInRect:CGRectMake(, h - - , , ) withAttributes:attr]; //左下角 UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return aimg; }
第二种方式:用drawInRect很方便,图片、文字都可以加
// 画水印 - (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) { UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen". } #else if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0) { UIGraphicsBeginImageContext([self size]); } #endif //原图 [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; //水印图 [mask drawInRect:rect]; UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newPic; }
以上叙述用两种方式实现IOS给图片添加水印,需要的朋友可以来参考下,希望大家能够喜欢。
这篇关于IOS给图片添加水印(两种方式)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码
- 2023-09-07iOS安全加固探讨:代码混淆、类名方法名混淆等方法
- 2023-09-05iOS代码加固与保护方法详解 - 提升iOS应用安全性的关键步骤