想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能。

1,效果图如下:
(在图片左上角和右下角都添加了文字。)
2,为方便使用,我们通过扩展UIImage类来实现添加水印功能
(文字大小,文字颜色,背景色,位置,边距都可以设置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//--- UIImageExtension.swift ---
import UIKit
 
extension UIImage{
     
    //水印位置枚举
    enum WaterMarkCorner{
        case TopLeft
        case TopRight
        case BottomLeft
        case BottomRight
    }
     
    //添加水印方法
    func waterMarkedImage(waterMarkText:String, corner:WaterMarkCorner = .BottomRight,
        margin:CGPoint = CGPoint(x: 20, y: 20), waterMarkTextColor:UIColor = UIColor.whiteColor(),
        waterMarkTextFont:UIFont = UIFont.systemFontOfSize(20),
        backgroundColor:UIColor = UIColor.clearColor()) -> UIImage{
         
        let textAttributes = [NSForegroundColorAttributeName:waterMarkTextColor,
            NSFontAttributeName:waterMarkTextFont]
        let textSize = NSString(string: waterMarkText).sizeWithAttributes(textAttributes)
        var textFrame = CGRectMake(0, 0, textSize.width, textSize.height)
         
        let imageSize = self.size
        switch corner{
        case .TopLeft:
            textFrame.origin = margin
        case .TopRight:
            textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x, y: margin.y)
        case .BottomLeft:
            textFrame.origin = CGPoint(x: margin.x, y: imageSize.height - textSize.height - margin.y)
        case .BottomRight:
            textFrame.origin = CGPoint(x: imageSize.width - textSize.width - margin.x,
                y: imageSize.height - textSize.height - margin.y)
        }
         
        // 开始给图片添加文字水印
        UIGraphicsBeginImageContext(imageSize)
        self.drawInRect(CGRectMake(0, 0, imageSize.width, imageSize.height))
        NSString(string: waterMarkText).drawInRect(textFrame, withAttributes: textAttributes)
         
        let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
         
        return waterMarkedImage
    }
}

3,使用样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import UIKit
 
class ViewController: UIViewController {
 
    @IBOutlet weak var imageView: UIImageView!
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //使用链式调用方法,给图片添加两条水印
        imageView.image = UIImage(named:"bg")?
            .waterMarkedImage("做最好的开发者知识平台")
            .waterMarkedImage("hangge.com", corner: .TopLeft,
                margin: CGPoint(x: 20, y: 20), waterMarkTextColor: UIColor.blackColor(),
                waterMarkTextFont: UIFont.systemFontOfSize(45), backgroundColor: UIColor.clearColor())
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)的更多相关文章

  1. PHP实现文字水印图片

    php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...

  2. 使用Qpaint在图片上写文字

    开发过程中需要实现在图片上叠加文字,可以采用Qpaint在图片上写文字,然后将图片显示在上面.再将Qlabel加到Qwidget中.效果如下 //创建对象,加载图片 QPixmap pix; pix. ...

  3. 函数putText()在图片上写文字

    #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace c ...

  4. thinkphp 利用GD库在图片上写文字

    <?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...

  5. thinkphp在为图片添加png水印不足的处理

    thinkphp在为图片加水印的时候.如果水印图片是png图片,透明度处理很不理想,与是做以下处理 在Image.class.php中新增 static function imagecopymerge ...

  6. 用python给图片添加半透明水印

    # coding:utf-8 from PIL import Image, ImageDraw, ImageFont def add_text_to_image(image, text): font ...

  7. python 图片格式转换png转jpg,如何利用python给图片添加半透明水印

    from PIL import Imageim = Image.open(r'd:\test2.png')r, g, b, a = im.split()im = Image.merge("R ...

  8. C#图片上写文字

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  9. vue 给 图片添加一个默认图片

    <img v-bind:src="userData.photo" :onerror="logo" class="img-box4"&g ...

随机推荐

  1. c++继承构造子类调用父类构造函数的问题及关于容器指针的问题及当容器里储存指针时,记得要手动释放

    看下面的一个问题: class Person { private: string name; public: Person(const string& s=""){ nam ...

  2. C# 调用迅雷 7 迅雷下载开放引擎

    最近有个项目需要用winform调用迅雷下载.上网百度后发现迅雷自带的com组件从迅雷5之后就废掉了,除了能添加任务,其余功能全不能用.后来又发现了迅雷下载开放引擎这么个东西 http://thund ...

  3. C#自学笔记总结

    一.变量:声明变量的语法:变量类型 变量名; 变量名=值;变量类型 变量名=值: 在使用变量的时候要注意:变量一定要先声明,再赋值,最后使用例子: 变量的特点:1.不能够被重复的声明2.可以被重复的赋 ...

  4. C++获取当前时间和计算程序运行时间的方法

    C++获取当前时间和计算程序运行时间的方法 获取当前时间: #include <iostream> #include <Windows.h> using namespace s ...

  5. ZOJ 2968 Difference Game 【贪心 + 二分】

    题意: 有Ga.Gb两堆数字,初始时两堆数量相同.从一一堆中移一一个数字到另一一堆的花费定义为两堆之间数 量差的绝对值,初始时共有钱C.求移动后Ga的最小小值减Gb的最大大值可能的最大大值. 思路: ...

  6. HDU2138 随机素数测试 Miller-Rabin算法

    题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...

  7. 鼠标滑轮一滚动Excel就停止工作

    鼠标滑轮一滚动Excel就停止工作 问题签名: 问题事件名称:APPCRASH 应用程序名:EXCEL.EXE 应用程序版本:15.0.4420.1017 应用程序时间戳:50673286 故障模块名 ...

  8. Struts2 学习笔记16 struts标签 part2

    接下来说一下if标签.下面是结果图. <li><s:if test="#parameters.age[0]<0">error!</s:if> ...

  9. poj 2114 Boatherds 树的分治

    还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...

  10. paip.输入法编程---输入法ATIaN历史记录 c823

    paip.输入法编程---输入法ATIaN历史记录 c823 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...