zoukankan      html  css  js  c++  java
  • UIImage 添加水印

    - (UIImage *)showWaterMaskInImage:(UIImage *)img WithText:(NSString *)text withlogoImage:(NSString *)waterMarkImg {
    float heightScale = SCREEN_HEIGHT/img.size.height/1.0;//图片的高度和屏幕高度的比例
    float widthScale = SCREEN_WIDTH/img.size.width/1.0;//图片的宽和屏幕宽的比例
    float scale = MIN(heightScale, widthScale);//选取比较小的比例
    float h = img.size.height*scale;//图片的高度乘以比例系数 == 水印图片的高度
    float w = img.size.width*scale;//图片的宽度乘以比例系数 == 水印图片的宽
    float fitScale = img.size.width/w;
    CGSize newFitSize = CGSizeMake(w*fitScale, h*fitScale);
    //生成要获取image的区域
    UIGraphicsBeginImageContext(newFitSize);
    // 获取当前图形上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 向下移动该图形上下文的座标原点
    CGContextTranslateCTM(context, 0, newFitSize.height);
    // 向上翻转图形上下文
    CGContextScaleCTM(context, 1.0, -1.0);
    // 创建显示区域
    CGRect imageframe = CGRectMake(0, 0, newFitSize.width, newFitSize.height);
    // 绘制图片
    CGContextDrawImage(context, imageframe, img.CGImage);
    [[UIColor darkGrayColor] set];//上下文种的文字属性
    CGContextTranslateCTM(context, 0, newFitSize.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    UIFont *font = [UIFont boldSystemFontOfSize:12*fitScale];
    //此处设置文字显示的位置
    [text drawInRect:CGRectMake(newFitSize.width - 95*fitScale, newFitSize.height - 22*fitScale, 90*fitScale, 12*fitScale) withFont:font];
    // [[UIImage imageNamed:waterMarkImg] drawInRect:CGRectMake(newFitSize.width - 115*fitScale, newFitSize.height - 22*fitScale, 15*fitScale, 15*fitScale)];
    // 从当前上下文种获取图片
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    //移除栈顶的基于当前位图的图形上下文。
    UIGraphicsEndImageContext();
    return image;
    }

    不用尺寸的图片上显示的水印是一样的尺寸

  • 相关阅读:
    我的2018:OCR、实习和秋招
    【OCR技术系列之六】文本检测CTPN的代码实现
    【OCR技术系列之五】自然场景文本检测技术综述(CTPN, SegLink, EAST)
    如何免费使用谷歌搜索
    CUDA编程之快速入门
    我在北京实习的四个月
    在C++98基础上学习C++11新特性
    Linux编程之线程池的设计与实现(C++98)
    ASP.NET Core中使用IOC三部曲(三.采用替换后的Autofac来实现AOP拦截)
    ASP.NET Core文件上传与下载(多种上传方式)
  • 原文地址:https://www.cnblogs.com/zrr-notes/p/7489835.html
Copyright © 2011-2022 走看看