美文网首页
iOS 给view添加文字或图片水印

iOS 给view添加文字或图片水印

作者: 简书笔记存档 | 来源:发表于2021-05-24 17:20 被阅读0次
    // 引用方式, 当前添加到window层的layer 实际应用时可根据具体情况添加响应的页面
    +(void)addWatermark:(NSString*)watermark{
        AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        UIWindow *window = appDelegate.window;
        
        if(!watermarkView){
            watermarkView = [UIView new];
            watermarkView.frame = window.bounds;
           [window.layer addSublayer:watermarkView.layer]
            watermarkView.userInteractionEnabled = NO;
        }
        
        if(watermark){
    //        UIImage *drawImg = [self watemarkWithText:watermark];
            UIImage *drawImg = [self waterAtImage:IMG(@"europe_lotarry") waterImgae:IMG(@"europe_lotarry") rect:CGRectMake(0, 0, 110, 69)];
            UIImage *rotageImg = [self rotatedImage:drawImg];
            watermarkView.backgroundColor = [UIColor colorWithPatternImage:rotageImg];
        }
    }
    // 添加文本水印
    + (UIImage*)watemarkWithText:(NSString*)watemarkText {
        UIFont *font = [UIFont systemFontOfSize:10.0];
        
        NSDictionary *attributes = @{NSFontAttributeName:font
                                     ,NSForegroundColorAttributeName:WATERMARK_COLOR
                                     ,NSKernAttributeName:@(3)       //字间距
                                     };
        
        //字符串的size
        CGSize size = [watemarkText sizeWithAttributes:attributes];
        //加上向上取整 ceilf()就能解决了
        CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
        
        UIGraphicsBeginImageContextWithOptions(adjustedSize, NO, 0);
    
        int w = adjustedSize.width;
        int h = adjustedSize.height;
        [watemarkText drawInRect:CGRectMake(0,0,w,h) withAttributes:attributes];
      
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    // 添加图片水印
    + (UIImage *)waterAtImage:(UIImage *)image
                 waterImgae:(UIImage *)waterImage
                       rect:(CGRect)rect {
        //开启图形上下文
        UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
        //绘制原图片
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.width)];
        //绘制水印
        [waterImage drawInRect:rect];
    //    [waterImage drawInRect:rect blendMode:kCGBlendModeNormal alpha:0.5];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    //使水印旋转45度。
    + (UIImage*)rotatedImage:(UIImage*)image
    {
        CGFloat rotation = -M_PI_4;
        
        //一行只有一个水印
        CGFloat w = 0;
        {
            CGFloat minW = image.size.width*M_SQRT2 + 20;
            CGFloat oneThirdScreen = [UIScreen mainScreen].bounds.size.width/3.0;
            w = fmax(minW,oneThirdScreen);
        }
        
        //界面最多显示4行水印
        CGFloat h = 0;
        {
            CGFloat minH = image.size.width*M_SQRT2 + 20;
            
            CGFloat quarterScreen = [UIScreen mainScreen].bounds.size.height/4.0;
            h = fmax(minH,quarterScreen);
        }
        
        CGRect destRect = CGRectMake(0, 0, w, h);
        CGSize destinationSize = destRect.size;
        
        UIGraphicsBeginImageContextWithOptions(destinationSize, NO, 0);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, destinationSize.width / 2.0f, destinationSize.height / 2.0f);
        CGContextRotateCTM(context, rotation);
        [image drawInRect:CGRectMake(-image.size.width / 2.0f
                                     , -image.size.height / 2.0f
                                     , image.size.width
                                     , image.size.height) blendMode:kCGBlendModePlusLighter alpha:.3];
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 给view添加文字或图片水印

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