1

I have two imageviews view1 and view2 having image1 and image2 respectively. view1 is bigger than view2.

It is oriented such that view2 is located inside view1. view2 is draggable as well.

How could i write a code to merge these two images in imageviews to a single image?

4

1 回答 1

0

You can merge two images using UIGraphicsBeginImageContext. Here is a small function written with Swift 2.1.1 which takes two images and creates a single image.

func mergeImages (forgroundImage : UIImage, backgroundImage : UIImage) {

    let bottomImage = forgroundImage
    let topImage = backgroundImage

    let size = backgroundImage.size
    UIGraphicsBeginImageContext(size)

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

    topImage.drawInRect(areaSize, blendMode: .Normal, alpha: 1.0)

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    resultImageView.image = newImage

    UIGraphicsEndImageContext()
}

You can call it like

mergeImages(yourImageViewOne.image!, backgroundImage: yourImageViewTwo.image!) // Call to mege images

And here is a link for further exploring.

于 2015-12-16T16:07:40.347 回答