水印
2018-04-24 本文已影响15人
child_cool
图片水印文字
图片水印文字class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let image = UIImage(named: "1.jpg")?.yj_watermark("cool", level: .center, vertical: .center, font: UIFont.boldSystemFont(ofSize: 100), color: UIColor.red)
imageView.image = image
}
}
extension String {
//计算文字大小
func textSize(_ font : UIFont , maxSize : CGSize) -> CGSize{
return self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font : font], context: nil).size
}
}
extension UIImage {
// 水平方向的位置
enum YJWaterMarkLevel {
case left
case right
case center
}
// 竖直方向的位置
enum YJWaterMarkVertical {
case top
case bottom
case center
}
func yj_watermark(_ text: String, level: YJWaterMarkLevel = .right, vertical: YJWaterMarkVertical = .bottom, font: UIFont = UIFont.boldSystemFont(ofSize: 40), color: UIColor = UIColor.red) -> UIImage? {
let textSize = text.textSize(font, maxSize: self.size)
let width = textSize.width
let height = textSize.height
var X : CGFloat = 0
var Y : CGFloat = 0
switch level {
case .left:
X = 0
case .center:
X = (self.size.width - width)/2.0
case .right:
X = self.size.width - width
}
switch vertical {
case .top:
Y = 0
case .center:
Y = (self.size.height - height)/2.0
case .bottom:
Y = self.size.height - height
}
UIGraphicsBeginImageContext(self.size)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
(text as NSString).draw(in: CGRect(x: X, y: Y, width: width, height: height), withAttributes: [NSAttributedStringKey.font : font, NSAttributedStringKey.foregroundColor: color])
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
图片水印图片
图片水印图片class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let image = UIImage(named: "1.jpg")?.yj_watermark(UIImage(named: "2.png")!, level: .center, vertical: .center)
imageView.image = image
}
}
extension String {
//计算文字大小
func textSize(_ font : UIFont , maxSize : CGSize) -> CGSize{
return self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSAttributedStringKey.font : font], context: nil).size
}
}
extension UIImage {
// 水平方向的位置
enum YJWaterMarkLevel {
case left
case right
case center
}
// 竖直方向的位置
enum YJWaterMarkVertical {
case top
case bottom
case center
}
func yj_watermark(_ image: UIImage, level: YJWaterMarkLevel = .right, vertical: YJWaterMarkVertical = .bottom) -> UIImage? {
// 水印图的尺寸
var width = image.size.width
var height = image.size.height
let scale = width / height
// 水印图的尺寸不允许超过父视图,任何一边超出自动按原比例缩放
if width > self.size.width { // 宽度超出
width = self.size.width
height = width / scale
}
if height > self.size.height { // 高度超出
height = self.size.height
height = height * scale
}
var X : CGFloat = 0
var Y : CGFloat = 0
switch level {
case .left:
X = 0
case .center:
X = (self.size.width - width)/2.0
case .right:
X = self.size.width - width
}
switch vertical {
case .top:
Y = 0
case .center:
Y = (self.size.height - height)/2.0
case .bottom:
Y = self.size.height - height
}
UIGraphicsBeginImageContextWithOptions(self.size, false, 0)
//UIGraphicsBeginImageContext(self.size)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
image.draw(in: CGRect(x: X, y: Y, width: width, height: height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func yj_watermark(_ text: String, level: YJWaterMarkLevel = .right, vertical: YJWaterMarkVertical = .bottom, font: UIFont = UIFont.boldSystemFont(ofSize: 40), color: UIColor = UIColor.red) -> UIImage? {
let textSize = text.textSize(font, maxSize: self.size)
let width = textSize.width
let height = textSize.height
var X : CGFloat = 0
var Y : CGFloat = 0
switch level {
case .left:
X = 0
case .center:
X = (self.size.width - width)/2.0
case .right:
X = self.size.width - width
}
switch vertical {
case .top:
Y = 0
case .center:
Y = (self.size.height - height)/2.0
case .bottom:
Y = self.size.height - height
}
UIGraphicsBeginImageContext(self.size)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
(text as NSString).draw(in: CGRect(x: X, y: Y, width: width, height: height), withAttributes: [NSAttributedStringKey.font : font, NSAttributedStringKey.foregroundColor: color])
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}