IOS | How to get image width / height at runtime

I use Aspect Fit' imageviewfull screen mode in Swift storyboard. The actual image is well suited to maintain a relationship. The problem is that I cannot get the actual width / height of the image at runtime.

When I use it imageview.frame.size.width, it returns the entire width of the screen as expected. When I use image.size.width, it returns a fixed image width (actual image width). But my intention is to get the width of the image at runtime, which is displayed in the user interface.

How to find it from the source.

EDIT 1

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

The current landscape screen with a label in the middle. The label width is the same for imageview, but the image width is not currently displayed.

enter image description here

+4
source share
1 answer

. iPhone 6S. , x . , 5S 4S, x . , , 9 18. , . !

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
    }


}
+1

All Articles