iOS图片添加水印

iOS添加水印的方法有很多,常用的做法就是直接将水印绘制到图片上。这种方式的缺陷是:如果绘制水印的地方不是纯色,就会出现不清楚的情况。解决的方式就是处理水印的每个像素点判断使用的颜色值。在 Demo 中通过四种方式 (普通模式,CPU计算,CIFilter方式,OpenGL方式) 进行处理,后续有时间会进行字符统一颜色值,目前只做了像素点的处理。中间一些问题记录如下作为备忘录。

通过CPU处理像素点时,第一思考方向是获取水印绘制的 Path ,然后通过 Path 计算经过的点。下面只进行文字 Path 的处理,通过 NSAttributedString 创建 CTFrameRef ,获取其中的 CTLineRef 信息,然后获取每行中的 CTRunRef 信息,通过 CTRunRef 信息获取字体信息 CTFontRef ,获取 CTRunRef 每一个图形信息 CGGlyph ,最后通过 CTFontCreatePathForGlyph 方法获取每个图形的 Path .

+ (NSArray *) pathsAboutStr:(NSString *)str
                       rect:(CGRect)rect
                       font:(UIFont*)font{
    NSMutableArray* retPaths = NSMutableArray.new;
    NSAttributedString *attrStr =
    [[NSAttributedString alloc]
     initWithString:str
     attributes:@{NSFontAttributeName: font}];
    CTFramesetterRef frameSetter =
    CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStr);
    //创建可变path
    CGMutablePathRef mutPath = CGPathCreateMutable();
    CGPathAddRect(mutPath, NULL, rect);
    //遍历数据,获取frame
    CTFrameRef frame =
    CTFramesetterCreateFrame(frameSetter,
                             CFRangeMake(0, str.length), mutPath, NULL);
    //获取行信息
    CFArrayRef lineRefs =
    CTFrameGetLines(frame);
    for (CFIndex lineIndex = 0; lineIndex < CFArrayGetCount(lineRefs); lineIndex++)
        CTLineRef aLineRef = (CTLineRef)CFArrayGetValueAtIndex(lineRefs, lineIndex);
        //获取Run信息
        CFArrayRef runRefs = CTLineGetGlyphRuns(aLineRef);
        for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runRefs); runIndex++)
            CTRunRef aRunRef =
            (CTRunRef)CFArrayGetValueAtIndex(runRefs, runIndex);
            NSArray* paths = [self pathsInRunRef:aRunRef];
            [retPaths addObjectsFromArray:paths];
    CGPathRelease(mutPath);
    return retPaths;
+ (NSArray*) pathsInRunRef:(CTRunRef)aRunRef{
    NSMutableArray* retPaths = NSMutableArray.new;
    CTFontRef fontRef = CFDictionaryGetValue(CTRunGetAttributes(aRunRef),
                                             kCTFontAttributeName);
    //获取Glyph信息
    for (CFIndex glyphIndex = 0; glyphIndex < CTRunGetGlyphCount(aRunRef); glyphIndex++){
        CFRange glyphRange = CFRangeMake(glyphIndex, 1);
        CGGlyph glyph;
        CGPoint position;
        CTRunGetGlyphs(aRunRef, glyphRange, &glyph);
        CTRunGetPositions(aRunRef, glyphRange, &position);
        //获取Path 然后获取点信息
        CGPathRef pathRef = CTFontCreatePathForGlyph(fontRef, glyph, NULL);
        [retPaths addObject:[UIBezierPath bezierPathWithCGPath:pathRef]];
        CGPathRelease(pathRef);
    return retPaths;

通过CGPath获取像素点,当处理到三次贝塞尔曲线时,需要大量的计算,放弃了这种方式。通过贝塞尔曲线控制点获取像素点的计算过程,参考结尾处的Refs。流程如下:

+ (NSArray*) pixelsInPathRef:(CGPathRef)aPathRef
    NSMutableArray* retPixels = NSMutableArray.new;
    CGPathApply(aPathRef, (__bridge void * _Nullable)(retPixels), CGPathApplier);
    return retPixels;
void CGPathApplier(void * __nullable info,
                   const CGPathElement *  element){
    NSMutableArray* retPixels = (__bridge NSMutableArray *)(info);
    //获取最后一个控制点
    CGPoint lastPoint = CGPointZero;
    NSValue* lastV = retPixels.lastObject;
    if (nil != lastV) {
        lastPoint = [lastV CGPointValue];
    switch (element->type) {
        case kCGPathElementMoveToPoint: {
            //添加一个点
            break;
        case kCGPathElementAddLineToPoint: {
            //直线处理
            //Y轴方向同轴,只做Y的递增处理
            //其他通过斜率计算
            break;
        case kCGPathElementAddQuadCurveToPoint: {
            //二次方贝塞尔曲线处理
            break;
        case kCGPathElementAddCurveToPoint: {
            //三次方贝塞尔曲线处理
            break;
        case kCGPathElementCloseSubpath: {
            break;

CIFilter注意区别参数类型__samplesampler。在CIColorKernel中因为只是处理材质当前位置的颜色信息,所以材质的类型为__sample ,为了提升性能通过优化后,__sample 只提供的rgba信息。

OpenGL处理中,iOS不能通过glRenderbufferStorage方法分配渲染显存,只能通过EAGLContextrenderbufferStorage:target fromDrawable:drawable方法分配,在通过glReadPixels读取像素信息时,必须设置drawablekEAGLDrawablePropertyRetainedBacking属性为true来防止渲染后擦除显存。显然这不是我们需要的,通过背景图大小创建纹理缓存,并在此缓存中进行渲染。

看下面的Refs比上面的好看多了^_^
Refs:
二次贝塞尔曲线处理