4

I am using 'Aspect Fit' mode imageview in a full screen in Swift storyboard. Actual image is fitting well keeping the ratio. Problem is i cannot get the actual image width/height on runtime.

When I use imageview.frame.size.width, it's returning the full screen width as expected. When I use image.size.width, it's returning the fixed image width (actual image width). But my intention is to get the runtime image width which is being shown in the UI.

How to find it from source.

EDIT 1

Current portrait screen with label in the middle enter image description here

Current landscape screen with label in the middle. Label width is same to imageview but not the currently displaying image width.

enter image description here

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • The actual `UIImage` object isn't resized. If you want to resize it, there are plenty of examples (including probably some on Stack Oveflow) that show you have to resize an image based on aspect fit or aspect fill. – nhgrif Apr 12 '16 at 12:00
  • @nhgrif Did you understand my requirement? if you have a solution better answer or refer with exact links. – Sazzad Hissain Khan Apr 13 '16 at 03:38
  • Yes. The actual `UIImage` object isn't resized, regardless of how it appears to be displayed. I am pointing this out, because it might be very important to know depending on what you're doing with this information. Importantly, unless you want to actually resize the underlying `UIImage` object (which would be a different question), I don't see how finding out its display size is valuable. – nhgrif Apr 13 '16 at 11:54
  • I need to fix the label width exactly same as image width. So how can i so it without knowing displaying image width? my apologies if i am on the wrong way... @nhgrif – Sazzad Hissain Khan Apr 13 '16 at 12:28
  • Then *exactly that* should be your question. Your current approach may be the best, or it may be an overcomplication. It's find to include what you have so far as your current approach, but your question should always be clear on what you are **actually** trying to do. – nhgrif Apr 13 '16 at 12:34
  • can you please show how to use your approach with sample code? @nhgrif – Sazzad Hissain Khan Apr 19 '16 at 05:20

1 Answers1

1

Try the following code. It works perfectly according to your requirement for iPhone 6S device. When device screen orientation changes to landscape mode, I am calculating the x position and width of the label frame. For other devices like 5S and 4S you need to calculate the x position and width according to the device height width ratio. For example, I used some constant value like 9 and 18. If you want to work your code for different devices, you need to use ratio factor. Hope this help!

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var theImageView: UIImageView!
@IBOutlet weak var theLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    theLabel.frame.size.width = (theImageView.image?.size.width)!

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}

override func viewDidAppear(animated: Bool) {

    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
            theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
            theLabel.frame.size.width = (theImageView.image?.size.height)!
        }
        else {
            theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
            theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
        }
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        theLabel.frame.size.width = theImageView.frame.size.width
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    //theImageView.frame.size.width = (theImageView.image?.size.width)!
    //theImageView.frame.size.height = (theImageView.image?.size.height)!
}

func rotated()
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
            theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
            theLabel.frame.size.width = (theImageView.image?.size.height)!
        }
        else {
            theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
            theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
        }
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        theLabel.frame.size.width = theImageView.frame.size.width
    }


}
Shekhar Gupta
  • 6,206
  • 3
  • 30
  • 50