-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImage+Overlay.swift
More file actions
35 lines (28 loc) · 1.21 KB
/
UIImage+Overlay.swift
File metadata and controls
35 lines (28 loc) · 1.21 KB
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
//
// UIImage+Overlay.swift
//
// Created by Matt Bridges on 3/6/16.
// Copyright © 2016 Matt Bridges. All rights reserved.
//
import Foundation
import UIKit
extension UIImage {
func applyOverlayWithColor(color: UIColor, blendMode: CGBlendMode) -> UIImage? {
// Create a new CGContext
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.mainScreen().scale)
let bounds = CGRect(origin: CGPointZero, size: self.size)
let context = UIGraphicsGetCurrentContext()
// Draw image into context, then fill using the proper color and blend mode
drawInRect(bounds, blendMode: .Normal, alpha: 1.0)
CGContextSetBlendMode(context, blendMode)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, bounds)
// Return the resulting image
let overlayImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return overlayImage
}
func applyOverlayWithColor(color: UIColor, blendMode: CGBlendMode, alpha: CGFloat) -> UIImage? {
return applyOverlayWithColor(color.colorWithAlphaComponent(alpha), blendMode: blendMode)
}
}