Search code examples
imagemergeswift3

Take 2 photos and merge them in swift 3


In swift 3, I would like to merge two images into a single combined image, like so:

this

The idea is to pick 2 photos from library or take them with camera. But then having done so, how can the two images be combined together programmatically as shown in the picture above?


Solution

  • This is simplest way to merge 2 photos in single ImageView:

    For Example:

        let LeftImage = UIImage(named: "LeftImage.png")
        let RightImage = UIImage(named: "RightImage.png")
    
        let size = CGSize(width: LeftImage!.size.width, height: LeftImage!.size.height + RightImage!.size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    
        LeftImage?.draw(in: CGRect(x: 0, y: 0, width:size.width/2, height: size.height))
        RightImage?.draw(in: CGRect(x: size.width/2, y: 0, width:size.width, height: size.height))
    
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        MergeImageView.image = newImage