0

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.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
  • What does ` But later on, when i access this self.imageView to share it, i only see original image without subview.` mean? When are you accessing it again? Please share all relevant code – impression7vx Jan 26 '21 at 14:15
  • I have edited to add my code, thanks for pointing out. – Umit Kaya Jan 26 '21 at 14:20
  • Ok.. where is the 2nd `UIImageView`? I only see one `UIImageView` on the first block of code with an assigned `UIImage` but no second `UIImageView`.? – impression7vx Jan 26 '21 at 14:23
  • I added first part also. Its an imageview assigned some random image. – Umit Kaya Jan 26 '21 at 14:56
  • You say "Here image has no subview" -- image will never have a subview, only `UIView`s will have subviews, not `UIImage`. My answer below indicates how you can leverage `UIImageView` subviews to find a subview `UIView` – impression7vx Jan 26 '21 at 15:12

2 Answers2

1

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

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • Will this reduce the shared image's quality/resolution? Because its a screenshot after all. – Umit Kaya Jan 26 '21 at 14:57
  • Yes, for a better way check the last atteched link. – Raja Kishan Jan 26 '21 at 15:20
  • I checked the attachment. Accepted answer is actually doing what i am almost doing here. Most voted answer works for my case but i have issue on sizing images (maybe it is not applicable in my case), therefore, there is a weird layout comes at the end. Do you have the idea how it can layout nicely? – Umit Kaya Jan 26 '21 at 15:59
  • @UmitKaya please attach the desired image. – Raja Kishan Jan 26 '21 at 16:02
  • https://ibb.co/B6QK06P is my desired image. White logo is the subview, and the background is the original image. I want to share it as it is seen in here. But when i access to self.imageView.image; white logo disappears. By the way i just saw your update, now im gonna try it. Thank you. – Umit Kaya Jan 26 '21 at 16:18
1
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.

impression7vx
  • 1,728
  • 1
  • 20
  • 50
  • why this? You already have demoStampImageView image view object. I think no required to add and retrive. – Raja Kishan Jan 26 '21 at 15:22
  • `demoStampImageView` is also a UIView at the end and it is only the subview. How would you use it in "let imageToShare = [ self.imageView.image ]" to get the expected final image? It doesnt fix the issue actually. – Umit Kaya Jan 26 '21 at 15:33
  • I've added a way to obtain the demo image. – impression7vx Jan 26 '21 at 15:46
  • Additionally, this obtains the original image, not a snapshot. – impression7vx Jan 26 '21 at 15:46
  • demoStampImageView is a UIView. How do you get image from UIView? – Umit Kaya Jan 26 '21 at 15:56
  • Added again, my apologies, had to type cast – impression7vx Jan 26 '21 at 16:18
  • Thank you very much @impression7vx It works. But i used the solution 'Raja Kishan' suggested as it suits my purpose better as i no need to search through subviews everytime i need to use the image. Bec. sometimes i wont add subview and i will need the raw image. Anyway, i appreciate your time and effort. Thanks again. – Umit Kaya Jan 26 '21 at 16:41