想要给图片添加文字水印或者注释,我们需要实现在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. Xamarin.Android开发实践(一)

    原文:Xamarin.Android开发实践(一) 一.准备工作 1.创建一个空的解决方案,并命名为Phoneword 2.右击解决方案 新建->新建项目 并命名为Phoneword_Droid ...

  2. IT第二天 - JAVA环境的配置、Hello的编写

    IT第二天 上午 HTML的一些讲解 下午 JDK的安配置 JAVA语法的注意事项 Hello的编写 晚上 作业 对println的应用 笔记 1.Classpath环境变量的配置:因为DOS对于文件 ...

  3. ThinkPHP - 缓存使用

    用法: 需要使用不同的缓存方式的时候 需要重新初始化,如果不初始化直接调用的话,则会按照系统配置自动初始化.初始化的返回值,可以直接操作缓存: $cache = cache(array('type'= ...

  4. java笔记之数据类型

    java中一句连续的字符不能分开在两行中书写,如国太长可用“+”将这两个字符串连起来 文档注释是以“/**”开头,并在注释内容末尾以“*/”结束. 文档注释是对代码的解释说明,可以使用javadoc命 ...

  5. android术语笔记

    参考:http://blog.csdn.net/luoshengyang/article/details/6618363 http://blog.csdn.net/singwhatiwanna/art ...

  6. Oracle同义词 synonyms

    Oracle中的同义词: 总结:简单的一句话,Oracle中不同用户的表一般都只能够自己的所属的用户可以用,如果不想通过授权的方式授权给其他用户使用,那么创建表的时候在表名的前面加上 synonyms ...

  7. Linux怎么设置PostgreSQL远程访问

    原文链接: Linux怎么设置PostgreSQL远程访问 安装PostgreSQL数据库之后,默认是只接受本地访问连接.如果想在其他主机上访问PostgreSQL数据库服务器,就需要进行相应的配置. ...

  8. BNU 4067 求圆并

    好久没写过单组数据的题目了 QAQ 赤裸裸的模板题 #include <cstdio> #include <cstring> #include <iostream> ...

  9. (Problem 3)Largest prime factor

    The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...

  10. VirtualBox中的Ubuntu没有权限访问共享文件夹/media/sf_bak

    之前已经搞定可以自动共享文件夹了,但是现在发现无法去访问,非root用户下,使用“ls /media/sf_bak”提示没有权限,当然如果切换到root,是可以的. [解决过程]1.把普通用户名加入到 ...