当前位置: > > > Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)

Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)

(本文代码已升级至Swift4)

我前面写了篇文章讲解如何给图片添加文字水印,而如果想要添加图片类型的水印也很简单,只要把原来代码里添加文字的部分改成图片即可。

1,效果图如下:
(在图片左上角添加了一个半透明的logo图片)


2,为方便使用,我们通过扩展UIImage类来实现添加图片水印功能
(水印图片透明度,位置,边距都可以设置)
//--- UIImageExtension.swift ---
import UIKit

extension UIImage{
    
    //水印位置枚举
    enum WaterMarkCorner{
        case TopLeft
        case TopRight
        case BottomLeft
        case BottomRight
    }
    
    //添加图片水印方法
    func waterMarkedImage(waterMarkImage:UIImage, corner:WaterMarkCorner = .BottomRight,
                          margin:CGPoint = CGPoint(x: 20, y: 20), alpha:CGFloat = 1) -> UIImage{
        
        var markFrame = CGRect(x:0, y: 0, width:waterMarkImage.size.width,
                               height: waterMarkImage.size.height)
        let imageSize = self.size
        
        switch corner{
        case .TopLeft:
            markFrame.origin = margin
        case .TopRight:
            markFrame.origin = CGPoint(x: imageSize.width - waterMarkImage.size.width - margin.x,
                                       y: margin.y)
        case .BottomLeft:
            markFrame.origin = CGPoint(x: margin.x,
                                       y: imageSize.height - waterMarkImage.size.height - margin.y)
        case .BottomRight:
            markFrame.origin = CGPoint(x: imageSize.width - waterMarkImage.size.width - margin.x,
                                       y: imageSize.height - waterMarkImage.size.height - margin.y)
        }
        
        // 开始给图片添加图片
        UIGraphicsBeginImageContext(imageSize)
        self.draw(in: CGRect(x: 0, y:0, width: imageSize.width, height: imageSize.height))
        waterMarkImage.draw(in: markFrame, blendMode: .normal, alpha: alpha)
        let waterMarkedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return waterMarkedImage!
    }
}

3,使用样例
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //给图片添加水印
        imageView.image = UIImage(named:"bg")?
            .waterMarkedImage(waterMarkImage: UIImage(named: "logo")!,
                              corner: .TopLeft,
                              margin: CGPoint(x: 20, y: 20), alpha:1)
    }
}
评论2
  • 2楼
    2016-03-10 17:44
    hangge999

    嗯,看懂了。解释的很清楚,感谢站长!!

    站长回复

    不客气 :)

  • 1楼
    2016-03-08 08:57
    hangge999

    在ViewController里
    imageView.image = UIImage(named:"bg")?
    .waterMarkedImage(UIImage(named: "logo7")!,corner: .TopLeft,
    margin: CGPoint(x: 20, y: 20), alpha:1)

    这个.waterMarkedImage(UIImage(named: "logo7")!,corner: .TopLeft,
    margin: CGPoint(x: 20, y: 20), alpha:1)
    是什么语法啊,这个点就可以调用extension UIImage里的waterMarkedImage这个函数,是不是上一句有一个?这样就调用了这个函数,那这个问号是什么

    站长回复

    .waterMarkedImage(....这个是和上面连起来的,我换行了而已,其实就是:
    imageView.image = UIImage(named:"bg")? .waterMarkedImag.....
    问号是Swift的新特性-可选类型,上面代码相当于:
    if let image = UIImage(named:"bg") {
    imageView.image = image.waterMarkedImag.....
    }