Skip to content

Instantly share code, notes, and snippets.

@CBLR
Forked from A-Zak/uiimage_combine.swift
Created September 6, 2018 09:18
Show Gist options
  • Select an option

  • Save CBLR/d5c1d0c43ce0815b970e822ac957026c to your computer and use it in GitHub Desktop.

Select an option

Save CBLR/d5c1d0c43ce0815b970e822ac957026c to your computer and use it in GitHub Desktop.
UIImage extension to combine two images
extension UIImage {
class func imageByCombiningImage(firstImage: UIImage, withImage secondImage: UIImage) -> UIImage {
let newImageWidth = max(firstImage.size.width, secondImage.size.width )
let newImageHeight = max(firstImage.size.height, secondImage.size.height)
let newImageSize = CGSize(width : newImageWidth, height: newImageHeight)
UIGraphicsBeginImageContextWithOptions(newImageSize, false, UIScreen.mainScreen().scale)
let firstImageDrawX = round((newImageSize.width - firstImage.size.width ) / 2)
let firstImageDrawY = round((newImageSize.height - firstImage.size.height ) / 2)
let secondImageDrawX = round((newImageSize.width - secondImage.size.width ) / 2)
let secondImageDrawY = round((newImageSize.height - secondImage.size.height) / 2)
firstImage .drawAtPoint(CGPoint(x: firstImageDrawX, y: firstImageDrawY))
secondImage.drawAtPoint(CGPoint(x: secondImageDrawX, y: secondImageDrawY))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment