iOS | 如何快速获取运行时图像的宽度/高度

萨扎德(Sazzad Hissain Khan)

我正在Swift故事板上的全屏Aspect Fit模式下使用' '模式imageview实际图像非常适合保持比例。问题是我无法在运行时获取实际的图像宽度/高度。

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

如何从源头找到它。

编辑1

当前人像屏幕,中间带有标签 在此处输入图片说明

当前横向屏幕,中间带有标签。标签宽度与imageview当前显示的图像宽度相同,但不同。

在此处输入图片说明

塞曼塔·萨哈(Seemanta Saha)

请尝试以下代码。它可以根据您对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
    }


}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS Swift运行时如何工作

来自分类Dev

如何在运行时获取iOS设备CPU架构

来自分类Dev

如何在应用程序被杀死而不在 IOS 后台运行时获取通知负载

来自分类Dev

当应用未运行时,iOS 7背景获取

来自分类Dev

在运行时设置图像的宽度和高度

来自分类Dev

iOS在应用未运行时如何处理FCM

来自分类Dev

如何在IOS中的运行时添加静态tableviewcell

来自分类Dev

在iOS中,如何获取屏幕的高度和宽度,包括在横向模式下宽度大于高度?

来自分类Dev

iOS图像视图-高度等于宽度的大小

来自分类Dev

iOS 7:在运行时动态设置图像(用于背景图像或视图)?

来自分类Dev

iOS 9运行时nop陷阱

来自分类Dev

跟踪应用的运行时间iOS

来自分类Dev

调试模糊的 iOS 运行时错误?

来自分类Dev

为iOS 6.1构建并在iOS 7下运行时,如何更改UIBarButtonItem的TintColor?

来自分类Dev

在iOS 6而非iOS 7上运行时出错

来自分类Dev

如何在Android或iOS移动设备上运行Node.js运行时

来自分类Dev

当我们的应用程序快速运行时,检测显示在 iOS 设备屏幕上的推送通知

来自分类Dev

JavaFX如何在运行时获取BorderPane高度?

来自分类Dev

如何相对于iOS中的图像动态更改UI按钮的宽度和高度

来自分类Dev

如何在运行时确定iOS设备是否支持Bluetooth LE

来自分类Dev

如何在本地本机iOS应用中在运行时读取Info.Plist?

来自分类Dev

如何仅根据运行时系统iOS版本覆盖方法?

来自分类Dev

如何在运行时刷新localizable.strings或NSLocalizedString(key,comment)ios

来自分类Dev

如何使用react-native访问iOS环境变量或运行时参数?

来自分类Dev

如何在运行时正确修改iOS表格视图单元(添加/删除子视图?)

来自分类Dev

如何在iOS的运行时自定义子类实现?

来自分类Dev

如何在运行时刷新localizable.strings或NSLocalizedString(key,comment)ios

来自分类Dev

iOS:当我开始在 UITextfield 中输入时如何调用 API 运行时?

来自分类Dev

如何在iOS中快速显示图像?

Related 相关文章

  1. 1

    iOS Swift运行时如何工作

  2. 2

    如何在运行时获取iOS设备CPU架构

  3. 3

    如何在应用程序被杀死而不在 IOS 后台运行时获取通知负载

  4. 4

    当应用未运行时,iOS 7背景获取

  5. 5

    在运行时设置图像的宽度和高度

  6. 6

    iOS在应用未运行时如何处理FCM

  7. 7

    如何在IOS中的运行时添加静态tableviewcell

  8. 8

    在iOS中,如何获取屏幕的高度和宽度,包括在横向模式下宽度大于高度?

  9. 9

    iOS图像视图-高度等于宽度的大小

  10. 10

    iOS 7:在运行时动态设置图像(用于背景图像或视图)?

  11. 11

    iOS 9运行时nop陷阱

  12. 12

    跟踪应用的运行时间iOS

  13. 13

    调试模糊的 iOS 运行时错误?

  14. 14

    为iOS 6.1构建并在iOS 7下运行时,如何更改UIBarButtonItem的TintColor?

  15. 15

    在iOS 6而非iOS 7上运行时出错

  16. 16

    如何在Android或iOS移动设备上运行Node.js运行时

  17. 17

    当我们的应用程序快速运行时,检测显示在 iOS 设备屏幕上的推送通知

  18. 18

    JavaFX如何在运行时获取BorderPane高度?

  19. 19

    如何相对于iOS中的图像动态更改UI按钮的宽度和高度

  20. 20

    如何在运行时确定iOS设备是否支持Bluetooth LE

  21. 21

    如何在本地本机iOS应用中在运行时读取Info.Plist?

  22. 22

    如何仅根据运行时系统iOS版本覆盖方法?

  23. 23

    如何在运行时刷新localizable.strings或NSLocalizedString(key,comment)ios

  24. 24

    如何使用react-native访问iOS环境变量或运行时参数?

  25. 25

    如何在运行时正确修改iOS表格视图单元(添加/删除子视图?)

  26. 26

    如何在iOS的运行时自定义子类实现?

  27. 27

    如何在运行时刷新localizable.strings或NSLocalizedString(key,comment)ios

  28. 28

    iOS:当我开始在 UITextfield 中输入时如何调用 API 运行时?

  29. 29

    如何在iOS中快速显示图像?

热门标签

归档