iOS给图片添加水印 swift版本

let image = UIImage(named: "2.jpg")
let imageLogo = UIImage(named: "123.png")
// 添加图片的水印
// 1 开启一个位图上下文 位图上下文跟view无关
// size 是画板的尺寸
UIGraphicsBeginImageContext((image?.size)!)

// 2 给原始图片添加文字
image?.drawAtPoint(CGPointZero)
let str = "富士康科技股份责任公司@"
let mulDicts = NSMutableDictionary()
mulDicts[NSFontAttributeName] = UIFont.systemFontOfSize(32)
// mulDicts[NSBackgroundColorAttributeName] = UIColor.redColor()
mulDicts[NSForegroundColorAttributeName] = UIColor.greenColor()
NSString(string: str).drawAtPoint(CGPointZero, withAttributes: NSDictionary(dictionary: mulDicts) as? [String : AnyObject])
imageLogo?.drawAtPoint(CGPointZero)

// 3 从上下文中获取图片
let imageWater = UIGraphicsGetImageFromCurrentImageContext()

// 4 关闭上下文
UIGraphicsEndImageContext()

let imageView = UIImageView(frame: self.view.frame)
imageView.image = imageWater
imageView.contentMode = UIViewContentMode.ScaleAspectFit

view.addSubview(imageView)

你可能感兴趣的:(iOS给图片添加水印 swift版本)