当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS给图片添加水印(两种方式)

IOS给图片添加水印(两种方式)

2019年07月24日  | 移动技术网IT编程  | 我要评论

买马发446123,深圳西站列车时刻表,老师我想对你说诗歌

为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个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给图片添加水印,需要的朋友可以来参考下,希望大家能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网