IOS 图片加文字水印,显示中文,斜45°

首先在图片上加水印有两种方法。
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

}

注:这些代码是网上博客看的然后自己添加了一些代码和注释

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值