iOS中给图片加水印或文字的几种方法

本文介绍了在iOS中为图片添加水印的多种方法,包括使用Core Graphics直接绘制文字到图片上,以及通过图片叠加实现水印效果。代码示例详细展示了如何实现中文水印和半透明效果。

最近项目中在做实名认证,要给图片添加水印,在网上找了一些博客,但是鱼龙混杂,首先说明下,有的博客说的方法实现了一下发现不可以。现在我就把我找的可以实现的几种方法贴在下边即可。以下方法我都亲自试过可以:

为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个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;
 
 }


第二种方式: 合成图片了,推荐大家使用这种方法,(缺点:合成图片打,但可以缩小其尺寸,有点水印可以任意形状样式:找美工做就好了,还很容易做成透明的)

/**
 加半透明水印
 @param useImage 需要加水印的图片
 @param addImage1 水印不要传jpg类型图,支持png
 @returns 加好水印的图片
 */
- (UIImage *)addImage:(UIImage *)useImage addMsakImage:(UIImage *)maskImage
{
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
    {
        UIGraphicsBeginImageContextWithOptions(useImage.size ,NO, 0.0); // 0.0 for scale means "scale for device's main screen".
    }
#else
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
    {
        UIGraphicsBeginImageContext(useImage.size);
    } 
#endif
    [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];
    
    //四个参数为水印图片的位置
    [maskImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height/2)];
    [maskImage drawInRect:CGRectMake(0, useImage.size.height/2, useImage.size.width, useImage.size.height/2)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

下边还有两种方法:其中第一种还不支持中文,字符串不可传入中文,第二种logo水印的不支持jpg

  1. -(UIImage *)addText:(UIImage *)img text:(NSString *)text1   
  2. {   
  3. //上下文的大小   
  4. int w = img.size.width;   
  5. int h = img.size.height;   
  6. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//创建颜色   
  7. //创建上下文   
  8. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 844 * w, colorSpace, kCGImageAlphaPremultipliedFirst);   
  9. CGContextDrawImage(context, CGRectMake(00, w, h), img.CGImage);//将img绘至context上下文中   
  10. CGContextSetRGBFillColor(context, 0.01.01.01);//设置颜色   
  11. char* text = (charchar *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];   
  12. CGContextSelectFont(context, "Georgia"30, kCGEncodingMacRoman);//设置字体的大小   
  13. CGContextSetTextDrawingMode(context, kCGTextFill);//设置字体绘制方式   
  14. CGContextSetRGBFillColor(context, 255001);//设置字体绘制的颜色   
  15. CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text));//设置字体绘制的位置   
  16. //Create image ref from the context   
  17. CGImageRef imageMasked = CGBitmapContextCreateImage(context);//创建CGImage   
  18. CGContextRelease(context);   
  19. CGColorSpaceRelease(colorSpace);   
  20. return [UIImage imageWithCGImage:imageMasked];//获得添加水印后的图片   
  21. }  

在上面的方法中,我们可以看到,我们可以通过将图片和文字绘制到同一个上下文中,并且重新生成图片,所获得图片就是包括图片和文字。

另外在一些项目中我们可能还回用到图片叠加,比如打水印等功能,这种功能相对上面给图片添加文字更容易,只是在上下文中,绘制两张图片,然后重新生成,以达到图片的叠加、代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. -(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo   
  2. {   
  3. //get image width and height   
  4. int w = img.size.width;   
  5. int h = img.size.height;   
  6. int logoWidth = logo.size.width;   
  7. int logoHeight = logo.size.height;   
  8. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();   
  9. //create a graphic context with CGBitmapContextCreate   
  10. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 844 * w, colorSpace, kCGImageAlphaPremultipliedFirst);   
  11. CGContextDrawImage(context, CGRectMake(00, w, h), img.CGImage);   
  12. CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);   
  13. CGImageRef imageMasked = CGBitmapContextCreateImage(context);   
  14. CGContextRelease(context);   
  15. CGColorSpaceRelease(colorSpace);   
  16. return [UIImage imageWithCGImage:imageMasked];   
  17. // CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);   
  18. }  

对于图片叠加文字,和图片叠加图片,基本的原理是一样的,创建绘图上下文,然后在上下文中绘制图片或者文字,然后重新生成图片,以达到我们需要的效果。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值