How to merge two UIImages?

ios overlay image on image swift
uiimage draw
uiimageview blend mode
uigraphicsimagerenderer draw image
uiimage resize
uiview to uiimage
how to merge two images in android programmatically
setblendmode swift

I am trying to merge two different images and create a new one. This is the way I would like to do: I have this image (A):

It's a PNG image and I would like to merge this one with another image (B) which I took from the phone to create something like this:

I need a function who merge A with B creating C. The size must remain from the A image and the image B should auto adapt the size to fit into the polaroid (A). Is it possible to do that? Thank for your help!

UPDATE Just one thing, the image (A) is a square and the image i took is a 16:9, how can i fix that?? If i use your function the image (B) that i took become stretched!

Hope this may help you,

var bottomImage = UIImage(named: "bottom.png")
var topImage = UIImage(named: "top.png")

var size = CGSize(width: 300, height: 300)
UIGraphicsBeginImageContext(size)

let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
bottomImage!.draw(in: areaSize)

topImage!.draw(in: areaSize, blendMode: .normal, alpha: 0.8)

var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

All the Best :)

How to combine/ merge 2 images into 1, You can create graphics context and draw both images in it. You'll get an image result from both your source images combined. - (UIImage*)  Merging Two Images in Paint 3D. First, ensure you have the images you want to merge readily available on your computer. Then follow the steps below.

This way the overlay picture will be much cleaner

class func mergeImages(imageView: UIImageView) -> UIImage {
 UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0) 
 imageView.superview!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
 let image = UIGraphicsGetImageFromCurrentImageContext()
 UIGraphicsEndImageContext()

 return image 
}

Merge two UIImages into one, On a simulator with the original code, it takes about 1.52s on my machine. Since the base image won't get resized ( self.size is passed in self.draw(in:) ), and its  Combine two images into one online, compare, place one image on the right or bottom from another. Auto-merge two pictures into one - IMG online IMGonline.com.ua

Swift 4 UIImage extension that enables easy image merging / overlaying.

extension UIImage { 

  func overlayWith(image: UIImage, posX: CGFloat, posY: CGFloat) -> UIImage {
    let newWidth = size.width < posX + image.size.width ? posX + image.size.width : size.width
    let newHeight = size.height < posY + image.size.height ? posY + image.size.height : size.height
    let newSize = CGSize(width: newWidth, height: newHeight)

    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    draw(in: CGRect(origin: CGPoint.zero, size: size))
    image.draw(in: CGRect(origin: CGPoint(x: posX, y: posY), size: image.size))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return newImage
  }

}

Merging two UIImages & generating a BASE64 String…, Hey guys! I recently came across a situation where I had to merge 2 UIImages into one and generate a BASE64 String of the resultant image, without losing the​  In this article, I’ll walk you through the steps to merge two pictures into one in Windows. Combine Two Images in MS Paint. First, let’s start with the images we want to combine. In my example, I will be using two of the sample photos that Microsoft includes with Windows 7: Chrysanthemum and Hydrangeas.

Swift 5: Extension for UIImage

extension UIImage {
  func mergeWith(topImage: UIImage) -> UIImage {
    let bottomImage = self

    UIGraphicsBeginImageContext(size)

    let areaSize = CGRect(x: 0, y: 0, width: bottomImage.size.width, height: bottomImage.size.height)
    bottomImage.draw(in: areaSize)

    topImage.draw(in: areaSize, blendMode: .normal, alpha: 1.0)

    let mergedImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return mergedImage
  }
}

UIImage extension to combine two images · GitHub, Updated for Swift 3.x extension UIImage { class func imageByCombiningImage(​firstImage: UIImage, withImage secondImage: UIImage)  How to Print to a PDF File in Windows 10. To combine a group of images into a PDF file in Windows 10, first you need to make sure your files are listed in File Explorer in the order you want them to appear in the PDF file. You might have to rename them so they get sorted the way you want.

Slightly modified version of answer by budidino. This implementation also handles negative posX and posY correctly.

extension UIImage {
    func overlayWith(image: UIImage, posX: CGFloat, posY: CGFloat) -> UIImage {
        let newWidth = posX < 0 ? abs(posX) + max(self.size.width, image.size.width) :
            size.width < posX + image.size.width ? posX + image.size.width : size.width
        let newHeight = posY < 0 ? abs(posY) + max(size.height, image.size.height) :
            size.height < posY + image.size.height ? posY + image.size.height : size.height
        let newSize = CGSize(width: newWidth, height: newHeight)

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        let originalPoint = CGPoint(x: posX < 0 ? abs(posX) : 0, y: posY < 0 ? abs(posY) : 0)
        self.draw(in: CGRect(origin: originalPoint, size: self.size))
        let overLayPoint = CGPoint(x: posX < 0 ? 0 : posX, y: posY < 0 ? 0 : posY)
        image.draw(in: CGRect(origin: overLayPoint, size: image.size))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        return newImage
    }
}

iPhone/iPad Tip: How to merge two images into a single image , Merging two images to create one single image file. draw in rect UIImage* imageLeft = [UIImage imageNamed:@"file1.png"]; CGRect rect = CGRectMake(0, 0,  Merge images has three modes. The first is vertical merge. The picture is merged into a picture from top to bottom. The second is horizontal merging, which is merged into a picture from left to right. The third is a fixed number of columns. If the number of fixed columns is 3, 3 pictures are merged from left to right. Then the next line, then merge 3 pictures from left to right.

How to use Core Graphics blend modes to draw a UIImage differently, To give you an idea what's possible, here's some code that takes two UIImages and draws them into one single image. The first image is drawn  Open both the source image (the one where the content you want currently resides) and the target image (the one that will receive the copied content) in your image editor. This figure shows a source and target image on in Photoshop. In the source image, use a selection tool to select the content that you want to copy.

How to merge two UIImages?, I am trying to merge two different images and create a new one. This is the way I would like to do: I have this image (A): polaroid. It's a PNG image and I would  Merge two images vertically or horizontally to create a new image, you can choose the thickness and color of the border. How to position the images. Vertically Horizontally. How to adjust the size of the images. Do not adjust Magnify the smallest Reduce the biggest Crop the biggest. Reduce the biggest. Constrain proportions (keep aspect radio

How To Combine Or Merge Two Images In Swift 4.2 and XCode , 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. -(void) UIImage_Merging_two_images{. UIImage *bottomImage = [UIImage  Merging two photos is something you can easily do in Microsoft Paint in Windows 8.1 -- or it can be quite difficult, depending on what kind of effect you want to achieve. Creating a split-screen effect is one of the easier options, which puts two photos together either side-by-side or one on top of the other.

Comments
  • can you try to change the alpha value to 1.0 or 0.1.. am not sure about the value
  • You can use different version of drawInRect which is not taking blending option as argument which is topImage!.drawInRect(CGRectMake(xPosition, yPosition, topImage!.size.width, topImage!.size.height))
  • just one thing, the image (A) is a square and the image i took is a 16:9, how can i fix that?? If i use your function the image (B) that i took become stretched!
  • I used this and generated a base64 string of the new image. and its resulting in a blank image. I checked it on codebeautify.org/base64-to-image-converter#. The image results in a blank white image. What i am trying to do is merge two image parallel to each other. Can someone help me out please ?
  • Problem: I got with space on top, bottom when UIImageView have Frame(10,10, width, height) Solution: Add UIImageView into the view so it will become the Frame(0, 0, width, height) and Fixed the white space top and bottom.
  • Thanks this is a nice one, but it does not handle negative posX or posY correctly. I'll add an answer that takes them into account.
  • Whomsoever has down-voted my answer, could you also take a moment and add your comment why its down-voted, so that I will also get to know what was wrong with my answer.
  • @luca-alberto, here is your solution!!