首先在图片上加水印有两种方法。
1.使用drawInRect直接添加
2.使用quartz 2D绘图
一,如果使用drawInRect添加水印,挺方便的
UIGraphicsBeginImageContext(img.size);//
[img drawInRect:CGRectMake(0,0 , w, h)];//首先添加图片
NSDictionary *attr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:], //设置字体
NSForegroundColorAttributeName : [UIColor redColor] //设置字体颜色
};//设置文字样式
[mark drawInRect:CGRectMake(, , , ) withAttributes:attr]; // 添加文字
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如果只是简单添加水印使用drawInRect就可以了
二 使用quartz 2D绘图
使用CGContextShowTextAtPoint显示中文会乱码
所以使用CGContextShowGlyphsAtPoint这个来显示中文,
使用这个方法要取得一个参数CGGlyph,我们可以使用coreText中的方法来获取
(UIImage )watermarkImage:(UIImage )img withName:(NSString *)name withFontSize:(float)fontsize
{NSString* mark = name;
int w = img.size.width;
int h = img.size.height;
int hypotenuse = 0;//画布对角边长
float font = fontsize;
hypotenuse = sqrt(w*w + h*h);
name = [name stringByAppendingString:@” “];
int i = 0;
while (i < 10)
{//水印循环布满整个屏幕
i++;CGSize detailSize = [name sizeWithFont:[UIFont systemFontOfSize:font] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; if (detailSize.width > hypotenuse) { break; } name = [name stringByAppendingString:name];
}
UIGraphicsBeginImageContext(img.size);
[img drawInRect:CGRectMake(0, 0, w, h)];
CGContextRef context = UIGraphicsGetCurrentContext();
MyDrawText(context, CGRectMake(0, 0, w, h),name,font);
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}
void MyDrawText (CGContextRef myContext, CGRect contextRect ,NSString *waterStr,float font) // 1
{
float w, h;
w = contextRect.size.width;
h = contextRect.size.height;
CGAffineTransform myTextTransform; // 2
CGContextSetCharacterSpacing (myContext, 0); // 4
CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); // 5
CGContextSetRGBFillColor (myContext, 0, 0, 1, 0.55); // 6
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 0.55); // 7
myTextTransform = CGAffineTransformMakeRotation ((M_PI * 0.25)); // 8选择文字M_PI为π-180°
CGContextSetTextMatrix (myContext, myTextTransform); // 9
//
UniChar *characters;
CGGlyph *glyphs;
CFIndex count;
CTFontRef ctFont = CTFontCreateWithName(CFSTR("STHeitiSC-Light"), 20.0, NULL);//
CTFontDescriptorRef ctFontDesRef = CTFontCopyFontDescriptor(ctFont);
CFNumberRef pointSizeRef = (CFNumberRef)CTFontDescriptorCopyAttribute(ctFontDesRef,kCTFontSizeAttribute);
CGFontRef cgFont = CTFontCopyGraphicsFont(ctFont,&ctFontDesRef );
CGContextSetFont(myContext, cgFont);
CGFloat fontSize = 20.0;
NSString* str2 = waterStr;
CFNumberGetValue(pointSizeRef, kCFNumberCGFloatType,&fontSize);
CGContextSetFontSize(myContext, 200.0);//z字体大小并不是字号
CGContextSetAlpha(myContext, 0.55);
count = CFStringGetLength((CFStringRef)str2);
characters = (UniChar *)malloc(sizeof(UniChar) * count);
glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * count);
CFStringGetCharacters((CFStringRef)str2, CFRangeMake(0, count), characters);
CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, count);
CGContextScaleCTM(myContext, 1, -1);//画出来的文字会颠倒,使用这个方法给倒回来,参数意思为真正绘图坐标 = 参数*设置的坐标
CGContextMoveToPoint(myContext, w/2, h/2);
CGContextShowGlyphsAtPoint(myContext, 0, -h/2, glyphs, str2.length);
CGContextShowGlyphsAtPoint(myContext, 0, -h, glyphs, str2.length);
CGContextShowGlyphsAtPoint(myContext, h /2, -h, glyphs, str2.length);
free(characters);
free(glyphs);//
// CGContextShowTextAtPoint(myContext, 40, 40, "only english", 9); // 10
}
注:这些代码是网上博客看的然后自己添加了一些代码和注释