Reputation: 5951
I have a UIImageView
which has another added UIImageView
on top as subview, seen below:
self.imageView.image = UIImage(named: "original_image.png")
let demoStampImage = UIImage(named: "demo_stamp.png")
let frame = CGRect(x: (UIScreen.main.bounds.width / 2) - 50, y: (UIScreen.main.bounds.height / 2) - 50, width: 100, height: 100)
let demoStampImageView = UIImageView(image: demoStampImage)
demoStampImageView.frame = frame
self.imageView.addSubview(demoStampImageView)
set in viewDidLoad
and i can see it nicely on the screen. But later on, when i access this self.imageView
to share it, i only see original image without subview, seen below.
@objc func shareIt() {
let imageToShare = [ self.imageView.image ] -> Here image has no subview
//Code goes on...
}
Please help me, what could be the cause and what can overcome this issue? I appreciate it all.
Upvotes: 0
Views: 455
Reputation: 1873
self.imageView = UIImageView()
self.imageView.image = UIImage(named: "original_image.png")
let demoStampImage = UIImage(named: "demo_stamp.png")
let frame = CGRect(x: (UIScreen.main.bounds.width / 2) - 50, y: (UIScreen.main.bounds.height / 2) - 50, width: 100, height: 100)
let demoStampImageView = UIImageView(image: demoStampImage)
demoStampImageView.tag = 1 //Define tag to find it later
demoStampImageView.frame = frame
self.imageView.addSubview(demoStampImageView)
let imageViewSubviews = self.imageView.subviews.filter{$0.tag == 1}
let demoStampImageView = imageViewSubviews[0] as! UIImageView//This is the only view with tag == 1
let demoImage = demoStampImageView.image
Please understand I am forcing the type case to UIImageView
without error handling. This should work, but forcing anything has potential downfalls and can cause issues.
Upvotes: 1
Reputation: 19004
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
Upvotes: 1