使用大容量存储器的快速处理

问题描述 投票:0回答:1

我正在尝试在容器视图中创建和布局图像。我收到内存不足警告,并将此代码添加到视图控制器时必须强制退出Xcode。

  private let sw: CGFloat = UIScreen.main.bounds.width
  private let sh: CGFloat = UIScreen.main.bounds.height

  private let iw: CGFloat = 340.0
  private let ih: CGFloat = 400.0

  private lazy var widthS: CGFloat = { (0.7 * sw) - (2 * 20.0) }()
  private lazy var heightS: CGFloat = { (widthS * ih) / iw }()

  private let maxHeightL: CGFloat = 450.0
  private lazy var heightL: CGFloat = { min(maxHeightL, sh * 0.5) }()
  private lazy var widthL: CGFloat = { (heightL * iw) / ih }()

而且我有一个设置功能,可以在其中设置该图像并使用约束对其进行布局。

private func setupIV() {
    img = getImageFromDB()
    iv = UIImageView(image: img)
    iv.translatesAutoresizingMaskIntoConstraints = false
    iv.contentMode = .scaleAspectFit

    ivContainer.addSubview(iv)

    let height: CGFloat = deviceType == .phone ? heightS : heightL

let topMargin: CGFloat = (UIDevice.current.screenType == .i5) ? 0.0
  : (UIDevice.current.screenType == .i6) ? 20.0
  : (UIDevice.current.screenType == .i6P) ? 30.0
  : (UIDevice.current.screenType == .ix) ? 50.0
  : (UIDevice.current.screenType == .iXR) ? 60.0
  : (UIDevice.current.screenType == .iXSMax) ? 80.0
  : (UIDevice.current.screenType == .p129) ? 40.0
  : 0.0

    iv.topAnchor.constraint(equalTo: iContainer.topAnchor, constant: topMargin).isActive = true
    iv.bottomAnchor.constraint(equalTo: iContainer.bottomAnchor, constant: -topMargin).isActive = true
    iv.centerXAnchor.constraint(equalTo: iContainer.centerXAnchor).isActive = true
    iv.centerYAnchor.constraint(equalTo: iContainer.centerYAnchor).isActive = true
    iv.heightAnchor.constraint(equalToConstant: height).isActive = true
  }

当我检查活动监视器时,雨燕正在消耗90GB的内存。

enter image description here

我正在使用xcode v版本11.3.1(11C504)

这与这里的计算有关吗?谢谢

ios swift xcode uiimageview low-memory
1个回答
0
投票

问题是我正在使用多个三元条件运算符。当我使用switch而不是嵌套的三元条件运算符时,解决了内存不足的问题。

我从此打开了我的图像视图布局的topMargin设置:

let topMargin: CGFloat = (UIDevice.current.screenType == .i5) ? 0.0
  : (UIDevice.current.screenType == .i6) ? 20.0
  : (UIDevice.current.screenType == .i6P) ? 30.0
  : (UIDevice.current.screenType == .ix) ? 50.0
  : (UIDevice.current.screenType == .iXR) ? 60.0
  : (UIDevice.current.screenType == .iXSMax) ? 80.0
  : (UIDevice.current.screenType == .p129) ? 40.0
  : 0.0

至此:

switch UIDevice.current.screenType {
case .i5:
  topPadding = 0.0
case .i6:
  topPadding = 20.0
case .i6P:
  topPadding = 30.0
case .ix:
  topPadding = 50.0
case .iXR:
  topPadding = 60.0
case .iXSMax:
  topPadding = 80.0
case .p129:
  topPadding = 40.0
default:
  topPadding = 0.0
}

swift,SourceKitService或XCBBuildService的内存使用现在大约为600 MB或小于1 GB(如果使用嵌套三元条件,则为90 GB)。

© www.soinside.com 2019 - 2024. All rights reserved.