最近项目中在做实名认证,要给图片添加水印,在网上找了一些博客,但是鱼龙混杂,首先说明下,有的博客说的方法实现了一下发现不可以。现在我就把我找的可以实现的几种方法贴在下边即可。以下方法我都亲自试过可以:
为了防止自己辛苦做的项目被别人盗走,采取图片添加水印,在此表示图片的独一无二。加水印不是在上面添加几个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
- -(UIImage *)addText:(UIImage *)img text:(NSString *)text1
- {
- //上下文的大小
- int w = img.size.width;
- int h = img.size.height;
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//创建颜色
- //创建上下文
- CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
- CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);//将img绘至context上下文中
- CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);//设置颜色
- char* text = (charchar *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
- CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);//设置字体的大小
- CGContextSetTextDrawingMode(context, kCGTextFill);//设置字体绘制方式
- CGContextSetRGBFillColor(context, 255, 0, 0, 1);//设置字体绘制的颜色
- CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text));//设置字体绘制的位置
- //Create image ref from the context
- CGImageRef imageMasked = CGBitmapContextCreateImage(context);//创建CGImage
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
- return [UIImage imageWithCGImage:imageMasked];//获得添加水印后的图片
- }
在上面的方法中,我们可以看到,我们可以通过将图片和文字绘制到同一个上下文中,并且重新生成图片,所获得图片就是包括图片和文字。
另外在一些项目中我们可能还回用到图片叠加,比如打水印等功能,这种功能相对上面给图片添加文字更容易,只是在上下文中,绘制两张图片,然后重新生成,以达到图片的叠加、代码如下:
- -(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
- {
- //get image width and height
- int w = img.size.width;
- int h = img.size.height;
- int logoWidth = logo.size.width;
- int logoHeight = logo.size.height;
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- //create a graphic context with CGBitmapContextCreate
- CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
- CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
- CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);
- CGImageRef imageMasked = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
- return [UIImage imageWithCGImage:imageMasked];
- // CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);
- }
对于图片叠加文字,和图片叠加图片,基本的原理是一样的,创建绘图上下文,然后在上下文中绘制图片或者文字,然后重新生成图片,以达到我们需要的效果。
本文介绍了在iOS中为图片添加水印的多种方法,包括使用Core Graphics直接绘制文字到图片上,以及通过图片叠加实现水印效果。代码示例详细展示了如何实现中文水印和半透明效果。
722

被折叠的 条评论
为什么被折叠?



