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 UIKitextension 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 UIKitclass 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 ...
随机推荐
- Vim中如何全选并复制?
全部删除:按esc后,然后dG全部复制:按esc后,然后ggyG 全选高亮显示:按esc后,然后ggvG(这个好像有点问题)或者ggVG正确 vim如何与剪贴板交互(将vim的内容复制出来) 习惯了在 ...
- 欧拉函数K - Relatives
欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...
- Python 字符、整型、列表字典等操作(二)
在上次课程中简要的讲述了Python的基础常识,现在来详细的学习一下吧! 一.类和对象 面向过程和面向对象 面向过程:C 面向对象:Java.Python等 类和对象的含义: 类,是对事物的抽象,比如 ...
- 高质量程序设计指南C/C++语言——C++/C程序设计入门(3)
- 手势滑动结束 Activity(一)基本功能的实现
喜欢听音乐的朋友可能都看过天天动听这款 app, 这款 app 有一个亮点就是在切换页面(Fragment)的时候能够通过手势滑动来结束当前页面.这里先说一下,我为什么会这么关心这个功能呢,由于前两天 ...
- PHP升级之后$SESSION丢失
要在生产环境为一个内部系统升PHP版本,由5.3升成5.4.16 生成以后发现不能login,一路打断点过去,发现服务器端两个页面跳转的时候,取不到$SESSION 悲催的上网找解决方案,结果发现各种 ...
- scanf()常犯错误
------------------------------------------------------------------------ <> 本意:接收字符串. 写成代码:voi ...
- mysql如何开启远程连接
链接地址:http://jingyan.baidu.com/article/046a7b3ed85f3ef9c27fa9dc.html 大家在公司工作中,经常会遇到mysql数据库存储于某个人的电脑上 ...
- Ibatis的分页机制的缺陷
我们知道,Ibatis为我们提供了可以直接实现分页的方法 queryForList(String statementName, Object parameterObject, int skipResu ...
- tomcat启动后ids页面无法访问
修改servers-->tomcat6.0-->server.xml <Context docBase="/tds7030-web" path="&qu ...