Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)
想要给图片添加文字水印或者注释,我们需要实现在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 - 给图片添加文字水印(图片上写文字,并可设置位置和样式)的更多相关文章
- PHP实现文字水印图片
php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...
- 使用Qpaint在图片上写文字
开发过程中需要实现在图片上叠加文字,可以采用Qpaint在图片上写文字,然后将图片显示在上面.再将Qlabel加到Qwidget中.效果如下 //创建对象,加载图片 QPixmap pix; pix. ...
- 函数putText()在图片上写文字
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace c ...
- thinkphp 利用GD库在图片上写文字
<?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...
- thinkphp在为图片添加png水印不足的处理
thinkphp在为图片加水印的时候.如果水印图片是png图片,透明度处理很不理想,与是做以下处理 在Image.class.php中新增 static function imagecopymerge ...
- 用python给图片添加半透明水印
# coding:utf-8 from PIL import Image, ImageDraw, ImageFont def add_text_to_image(image, text): font ...
- python 图片格式转换png转jpg,如何利用python给图片添加半透明水印
from PIL import Imageim = Image.open(r'd:\test2.png')r, g, b, a = im.split()im = Image.merge("R ...
- C#图片上写文字
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- vue 给 图片添加一个默认图片
<img v-bind:src="userData.photo" :onerror="logo" class="img-box4"&g ...
随机推荐
- 【组队赛三】-C cf448B
Suffix Structures Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit S ...
- C++ 表达式语句 海伦的故事
C++ 表达式语句 海伦的故事 摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 把今天当成最后一天来过.-海伦 请读者在 ...
- windows下取linux系统里面的文件
方法一:使用原生态的psftp 1.下载psftp.exe http://pan.baidu.com/s/1boVLHKF 2.双击psftp.exe 2.1 输入指令:open IP地址 (例如:o ...
- PHP - 直接输出对象的版本问题
- 【centos6 , 6】linux 查看帮助文档:
1. 使用 命令 -h 或 命令 --help , 例: ls -h 2. man命令 : man 命令 例:man ls 3.info命令: ...
- MYSQL 备份用户权限
MYSQL 备份用户权限 datadbblack 192.168.1.10 是 SELECT, RELOAD, SHOW DATABASES, LOCK TABLES 否 权限列表 1.Select ...
- tabbar动画切换
效果1: UIViewController *vc = self.viewControllers[self.selectedIndex]; CATransition *animation =[CATr ...
- 同步fifo的verilogHDL设计实例
原创 设计一个fifo,输入16bit,输出16bit的data,寻址宽度5bit,有空满标志. top 层如下所示: /* date : 2014/10/14 version : modelsim ...
- 转:Linus:利用二级指针删除单向链表
感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多,并加入了插图) Linus大婶在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是 ...
- 无线网络wifi (WPA/WPA2)密码破解方法
无线网络password破解WPA/WPA2教程 本教程用于探索无线路由安全漏洞,禁止用于非法用途,违者法律必究(与我无关) 在动手破解WPA/WPA2前,应该先了解一下基础知识,本文适合新手阅读 首 ...