想要给图片添加文字水印或者注释,我们需要实现在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. struts OGNL数据标签

    OGNL对象图导航语言,类似于el表达式,strut的底层就是用这个写的在导入struts-core的时候会导入ognl.jar public class Test { public static v ...

  2. cocos2dx进阶学习之CCLayer

    继承关系 CCLayer -> CCNode,CCTouchDelegate,CCAccelerometerDelegate,CCKeypadDelegate CCLayer在cocos2dx中 ...

  3. 首届全球RTB(实时竞价)广告DSP算法大赛

    首届全球RTB(实时竞价)广告DSP算法大赛 竞赛指南     RTB (Real Time Bidding, 实时竞价) 是近年来计算广告领域最激动人心的进展之一. 它增加了展示广告的透明度与效率, ...

  4. 瑞星杀毒软件、奇虎360杀毒软件、360卫士、百度卫士联手,搞不定弹出广告 &amp; 恶意广告图标

    一位网友说他的电脑近期出了问题:开机后桌面和任务栏上的高速启动栏会出现恶意图标.删除了下次开机又会出现:使用电脑过程中每分钟都会弹出广告.他为电脑安装了瑞星杀毒软件.奇虎360杀毒软件.360卫士.百 ...

  5. Linux下的定时器

    以下摘自linux下的man文件:(man  getitimer) #include  <sys/time.h> int  getitimer(int which,  struct iti ...

  6. ZOJ 3203 Light Bulb - 求导求最大值

    如果L全在地面上: 输出 h * D / H 如果L全在墙上: 输出 h 否则: (D - X ) / X = Y / (H - h) L = D - X + h - Y 然后对L求导即可 #incl ...

  7. Ural 1079 - Maximum

    Consider the sequence of numbers ai, i = 0, 1, 2, …, which satisfies the following requirements: a0  ...

  8. iPhone开发技巧之日志保存教程

    http://mobile.51cto.com/iphone-283337.htm Objective-C开发程序的时候,有专门的日志操作类NSLog,它将指定的输出到标准的错误输出上(stderr) ...

  9. Android 开发中eclipse 下 DDMS 视图中 sdcard 中文件导入的处理

    首先需要说明下,这里说的sdcard的权限并不是指在Android application程序中设置sdcard的权限读 取问题.而是指在DDMS看到的目录下的那个sdcard目录的权限问题.     ...

  10. C#委托好处知多少

    1.性能 性能是泛型的一个主要优点. 直接上例子,通过实例可以让我们很好的理解这一点. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start() ...