Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40156

iOS | How to get runtime image width/height in swift

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

Upvotes: 4

Views: 9003

Answers (1)

Seemanta Saha
Seemanta Saha

Reputation: 86

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
    }


}

Upvotes: 1

Related Questions