美文网首页
简单而又美观的添加全屏水印并截图

简单而又美观的添加全屏水印并截图

作者: airron | 来源:发表于2018-06-19 10:19 被阅读133次

    平时我们在实现加水印需求时,一般的思路是将指定 文字 或者指定 水印 logo 绘制在画布上然后截图来实现,就如以下代码:

    
    /**    
      * 加文字随意@param logoImage 需要加文字的图片@param watemarkText 文字描述@returns 加好文字的图片 
      */
    - (UIImage *)addWatemarkTextAfteriOS7_WithLogoImage:(UIImage *)logoImage watemarkText:(NSString *)watemarkText{    
        int w = logoImage.size.width;    
        int h = logoImage.size.height;    
        UIGraphicsBeginImageContext(logoImage.size);
        [[UIColor whiteColor] set];
        [logoImage drawInRect:CGRectMake(0, 0, w, h)];
        UIFont * font = [UIFont systemFontOfSize:18.0];
        [watemarkText drawInRect:CGRectMake(10, 55, 130, 80) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor whiteColor]}];
        UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    

    以上代码摘自:https://www.jianshu.com/p/b33812eecaf0

    这种实现方式能够为我们 添加单一而又简单的水印效果,但是如果我们需要添加全屏的水印该怎么做呢,难道要用上面的方法绘满屏幕吗? 当然这种方法在实现全屏水印的时候肯定是很笨拙的。这里给处的方法就是利用 简单的三行代码来实现。核心就是系统方法

    + (UIColor *)colorWithPatternImage:(UIImage *)image;
    

    对于该方法,官方给出的解释是

    ## Discussion
    
    You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.
    
    By default, the phase of the returned color is 0, which causes the top-left corner of the image to be aligned with the drawing origin. To change the phase, make the color the current color and then use the [`CGContext<wbr style="box-sizing: inherit;">Set<wbr style="box-sizing: inherit;">Pattern<wbr style="box-sizing: inherit;">Phase`](https://developer.apple.com/documentation/coregraphics/1455334-cgcontextsetpatternphase?language=objc) function to change the phase.
    
    

    使用该方法返回的Color 在绘制图像时,实际实现的是将 指定image平铺在画布上的效果。
    由此,我们只需三句代码即可实现平铺水印了。

    UIImage * strokImage = [UIImage imageNamed:@"watermark"];
            UIColor * strokColor = [UIColor colorWithPatternImage:strokImage];
            self.contentView.backgroundColor = strokColor;
    

    剩下的工作就剩下截屏了

    UIImage* image = nil;
        UIScrollView * scrollView = self.zoomControlScrollView;
        UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);
        CGPoint saveContentOffset = scrollView.contentOffset;
        CGRect saveFrame = scrollView.frame;
        
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
        
        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        scrollView.contentOffset = saveContentOffset;
        scrollView.frame = saveFrame;
        UIGraphicsEndImageContext();
    
    

    上述代码中我要截取的是一个ScrollView上的所有内容

    具体效果: IMG_1539.JPG 需要用到的水印素材: watermark.png

    上述截图 应用场景:https://itunes.apple.com/cn/app/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE-%E5%B9%82%E5%AE%9D%E7%B2%BE%E5%93%81-%E9%AB%98%E6%95%88%E5%B7%A5%E4%BD%9C%E5%AD%A6%E4%B9%A0%E7%9A%84%E8%84%91%E5%9B%BE%E6%B5%81%E7%A8%8B%E5%9B%BE%E5%B7%A5%E5%85%B7/id1369668984?mt=8

    TODO:上述的截图方法

    UIGraphicsGetImageFromCurrentImageContext()
    

    存在着内存问题,暂未找到合适的既能高清截图又不会导致内存暴涨的方法,如有小伙伴有更好的方法欢迎留言。

    相关文章

      网友评论

          本文标题:简单而又美观的添加全屏水印并截图

          本文链接:https://www.haomeiwen.com/subject/qtfaeftx.html