想要给图片添加文字水印或者注释,我们需要实现在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. 55. 略谈Lotus Notes的与众不同及系列文章至此的总结

    在二十多年的悠久历史里,Lotus Notes发展出一整套独特的概念.技术和思维.由于它早期惊人的领先时代和后续发展中同样惊人的忠于传统,这位软件领域的寿星在如今发展更新速度远超往日和技术愈趋公开互通 ...

  2. paip.php-gtk 桌面程序 helloworld总结

    paip.php-gtk 桌面程序 helloworld总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.cs ...

  3. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  5. 初步swift语言学习笔记2(可选类型?和隐式可选类型!)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28904115 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...

  6. 【Eclipse】Tomcat 一直处于starting状态,项目却已成功启动

    是因为Eclipse里面设置了代理.preference-network connections-activity provider-direct-应用.重启tomcat即可.

  7. 在storyboard中设置控件的layerbordercolor

    在SB中控件可以在SB中直接利用kvc 设置一些属性值,不如layerwidth等 但是不能更改和颜色有关的属性因为layerbordercolor是CGColor.通过为CALayer增加属性可以实 ...

  8. centos 内网ip访问mysql数据库

    参考博文: CentOS 配置mysql允许远程登录 Centos6.5 双网卡配置一个上外网一个接局域网  这个博文仅作参考 公司租用景安的服务器,给景安沟通配置内网. [root@bogon ng ...

  9. ELK 之二:ElasticSearch 和Logstash高级使用

    一:文档 官方文档地址:1.x版本和2.x版本 https://www.elastic.co/guide/en/elasticsearch/guide/index.html 硬件要求: 1.内存,官方 ...

  10. 【转】OpenCV中使用神经网络 CvANN_MLP

    原文见:http://blog.csdn.net/xiaowei_cqu/article/details/9027617 OpenCV的ml模块实现了人工神经网络(Artificial Neural ...