手机
当前位置:查字典教程网 >编程开发 >IOS开发 >IOS给图片添加水印(两种方式)
IOS给图片添加水印(两种方式)
摘要:为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个Label,而是我们把字画到图片上成为...

为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个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给图片添加水印(两种方式)】相关文章:

iOS 检测网络状态的两种方法

iOS实现图片保存与搜索功能

iOS 开发之 - 关闭键盘 退出键盘 的5种方式

iOS图片实现可拉伸不变形的处理操作

iOS图片模糊效果的实现方法

IOS实现输入验证码、密码按位分割(二)

IOS多线程编程的3种实现方法

iOS中NSArray数组常用处理方式

iOS毛玻璃效果的实现及图片模糊效果的三种方法

总结iOS实现渐变颜色的三种方法

精品推荐
分类导航