Created
November 16, 2017 09:09
-
-
Save Hexfire/beba832976c2839ff4be51808b27fdbf to your computer and use it in GitHub Desktop.
Merge two images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
static func imageByMergingImages(topImage: UIImage, bottomImage: UIImage, scaleForTop: CGFloat = 1.0) -> UIImage { | |
let size = bottomImage.size | |
let container = CGRect(x: 0, y: 0, width: size.width, height: size.height) | |
UIGraphicsBeginImageContextWithOptions(size, false, 2.0) | |
UIGraphicsGetCurrentContext()!.interpolationQuality = .high | |
bottomImage.draw(in: container) | |
let topWidth = size.width / scaleForTop | |
let topHeight = size.height / scaleForTop | |
let topX = (size.width / 2.0) - (topWidth / 2.0) | |
let topY = (size.height / 2.0) - (topHeight / 2.0) | |
topImage.draw(in: CGRect(x: topX, y: topY, width: topWidth, height: topHeight), blendMode: .normal, alpha: 1.0) | |
return UIGraphicsGetImageFromCurrentImageContext()! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment