Skip to content

Instantly share code, notes, and snippets.

@Hexfire
Created November 16, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexfire/beba832976c2839ff4be51808b27fdbf to your computer and use it in GitHub Desktop.
Save Hexfire/beba832976c2839ff4be51808b27fdbf to your computer and use it in GitHub Desktop.
Merge two images
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