Adding as subview does not mean two images are merged. For this, you need to first merge two images or need to take a screenshot of this image view.
Like this taking screenshot: (Note: Image size is dependent on image view size, for the original size you need to calculate frame according to the requirement.)
extension UIView {
func snapImage() -> UIImage {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
}
}
}
@objc func shareIt() {
let imageToShare = [ self.imageView.snapImage() ]
//Code goes on...
}
Edit
Or you can merge two images with the original size. Like this
extension UIImage {
func mearge(image: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
self.draw(in: CGRect(x:0 , y: 0, width: size.width, height: size.height))
let topImageFrame = CGRect(x: (size.width / 2) - 50, y: (size.height / 2) - 50, width: 100, height: 100)
image.draw(in: topImageFrame)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
@objc func shareIt() {
let imageToShare = [ self.imageView.image!.mearge(image:demoStampImage!) ]
//Code goes on...
}
See more for combine/merge 2 images: here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…