iOS |如何在swift中获取运行时图像的宽度/高度

iOS |如何在swift中获取运行时图像的宽度/高度,ios,swift,autolayout,imageview,Ios,Swift,Autolayout,Imageview,我正在Swift故事板中全屏使用“Aspect-Fit”模式imageview。实际图像拟合良好,保持了比例。问题是我无法在运行时获得实际的图像宽度/高度 当我使用imageview.frame.size.width时,它会返回预期的全屏宽度。当我使用image.size.width时,它返回固定的图像宽度(实际图像宽度)。但我的目的是获取UI中显示的运行时图像宽度 如何从源代码中找到它 编辑1 中间有标签的当前肖像屏幕 (P)目前的景观屏幕中间有标签。标签宽度与imageview相同,但与当

我正在Swift故事板中全屏使用“
Aspect-Fit
”模式
imageview
。实际图像拟合良好,保持了比例。问题是我无法在运行时获得实际的图像宽度/高度

当我使用
imageview.frame.size.width
时,它会返回预期的全屏宽度。当我使用
image.size.width
时,它返回固定的图像宽度(实际图像宽度)。但我的目的是获取UI中显示的运行时图像宽度

如何从源代码中找到它

编辑1

中间有标签的当前肖像屏幕

(P)目前的景观屏幕中间有标签。标签宽度与
imageview
相同,但与当前显示的图像宽度不同


请尝试以下代码。它完全符合您对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
    }


}

实际的
UIImage
对象未调整大小。如果您想调整图像大小,有很多示例(可能包括堆栈上的一些Oveflow)表明您必须根据纵横比调整图像大小。@nhgrif您了解我的要求吗?如果你有一个更好的解决方案,请用准确的链接回答或参考。是的。实际的
UIImage
对象不会调整大小,无论其显示方式如何。我指出这一点,因为根据您对这些信息的处理情况,了解这些信息可能非常重要。重要的是,除非您想实际调整基础
UIImage
对象的大小(这将是一个不同的问题),否则我看不出找出其显示大小有什么价值。我需要将标签宽度与图像宽度完全相同。那么,在不知道显示图像宽度的情况下,我如何才能这样做呢?如果我走错了路,我道歉@那应该是你的问题。您当前的方法可能是最好的,也可能是过于复杂。在你目前的方法中,最好包括你所拥有的东西,但是你的问题应该总是清楚你实际上在做什么。