Skip to content

Instantly share code, notes, and snippets.

@9bany
Created November 9, 2021 03:42
Show Gist options
  • Save 9bany/bd48b88023de7431b51e08206f5f4ed9 to your computer and use it in GitHub Desktop.
Save 9bany/bd48b88023de7431b51e08206f5f4ed9 to your computer and use it in GitHub Desktop.
Overlay image on the another image
extension UIImage{
func merge(mergewith: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let actualArea = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let ratioMerge: Double = Double(mergewith.size.height / mergewith.size.width)
let widthMerge = size.height / ratioMerge
let mergeX = (size.width / 2) - (widthMerge / 2)
let mergeArea = CGRect(x: mergeX, y: 0, width: widthMerge, height: size.height)
self.draw(in: actualArea)
mergewith.draw(in: mergeArea)
let merged = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return merged
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment