首页 技术 正文
技术 2022年11月7日
0 收藏 597 点赞 711 浏览 2565 个字

想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能。

1,效果图如下:(在图片左上角和右下角都添加了文字。)Swift – 给图片添加文字水印(图片上写文字,并可设置位置和样式)2,为方便使用,我们通过扩展UIImage类来实现添加水印功能(文字大小,文字颜色,背景色,位置,边距都可以设置)

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 //--- 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,使用样例

123456789101112131415161718192021 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()    }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731