【问题标题】:iOS app runs fine in Xcode simulator, but is slow and crashes on a deviceiOS 应用在 Xcode 模拟器中运行良好,但在设备上运行缓慢且崩溃
【发布时间】:2015-08-16 02:35:30
【问题描述】:

我有一个在模拟器中运行良好的应用程序,但是当我在设备上运行它时,应用程序运行缓慢并且偶尔会崩溃。当点击表格视图单元格并转换到新视图时,会特别出现此问题。当点击一个单元格时会有一两秒的延迟,那么新视图将以缓慢的延迟方式推送。这也是它偶尔会崩溃的地方。

该应用程序在设备上运行良好,直到最近对代码进行了一些更改,我认为这就是问题所在。我在下面添加了包含可疑代码的整个 viewDidLoad(在代码中表示)。

我注意到的另一件事是,调试导航器中的 CPU 使用率达到 130% 左右,而应用程序在 sim 卡中运行时会在设备上出现问题。

override func viewDidLoad() {
    super.viewDidLoad()

    // screen size for collection view cell
    screenSize = UIScreen.mainScreen().bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    dimView.alpha = 0
    openingHoursView.alpha = 0

    resDescriptionLabel.layer.borderColor = UIColor.blackColor().CGColor
    resDescriptionLabel.layer.borderWidth = 2
    resDescriptionLabel.layer.cornerRadius = 4

    //add logo to nav bar
    let navBarLogo: UIImageView = UIImageView(frame: CGRectMake(0, 0, 120, 36))
    navBarLogo.image = UIImage(named: "logo")
    self.navigationItem.titleView = navBarLogo

    self.resImage.image = UIImage(named: "placeholder")
    if let checkedUrl = NSURL(string: "http://staging.api.cheapeat.com.au/restaurants/\(self.venueID)/photo") {
        downloadImage(checkedUrl)
    }

    self.resName.text = " " + self.venueName + " "
    self.resAdd.text = " " + self.venueAdd + " "
    self.resPhone.text = "\(self.venuePH)"
    self.resPhoneNumber.text = self.venuePH
    self.resWebText.text = "\(self.venueWeb)"

    ///////////////////// new code starts here //////////////

    var paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.Justified
    paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping

    var attributedString = NSAttributedString(string: self.venueInfo,
        attributes: [
            NSParagraphStyleAttributeName: paragraphStyle,
            NSBaselineOffsetAttributeName: NSNumber(float: 0)

        ])

    self.resDescriptionLabel.attributedText = attributedString

    aboutVenueLabelWidthConstraint.constant = screenWidth - 16

    resDescriptionLabel.edgeInsets.left = 10
    resDescriptionLabel.edgeInsets.top = 10
    resDescriptionLabel.edgeInsets.right = 10
    resDescriptionLabel.edgeInsets.bottom = 10

    resDescriptionLabel.layoutIfNeeded()

    backgroundImageHeightConstraint.constant = resDescriptionLabel.bounds.height + 130

    // set content view height i.e. scrollable area
    // (dynamic height of label + combined height of all other content and contraints)
    contentViewHeightConstraint.constant = resDescriptionLabel.bounds.height + 790

    /////////////////// new code ends here ///////////////

    self.displayMap(self.venueLat, lng: self.venueLng)
    self.getArrayForCollection()
    self.getArrayValues()
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

        self.getDataForNextTable()
        })

    mon.text = timeFormatting(openingHours, day: "Monday")
    tue.text = timeFormatting(openingHours, day: "Tuesday")
    wed.text = timeFormatting(openingHours, day: "Wednesday")
    thu.text = timeFormatting(openingHours, day: "Thursday")
    fri.text = timeFormatting(openingHours, day: "Friday")
    sat.text = timeFormatting(openingHours, day: "Saturday")
    sun.text = timeFormatting(openingHours, day: "Sunday")
}

【问题讨论】:

  • 尝试一一去除约束
  • 另外,请确保 URL 处于活动状态,我去了它的根目录并显示它现在没有提供服务,但可能是他们以这种方式设置它以防止黑客攻击或其他什么。另外,我不确定你的函数“downloadImage(checkedUrl)”是做什么的,看看这个函数做了什么来确定这是否是罪魁祸首会很有帮助
  • 使用名为“Time Profiler”的 Xcode 工具 Instruments 来检测代码中的问题位置。

标签: ios performance swift uitableview crash


【解决方案1】:

我找到了罪魁祸首,事实证明它根本不在代码中。我在滚动视图中设置了一些高分辨率(5000x5000)的背景图像。他们绝对在消耗内存(在调试器中达到了大约 400mb)。我调整了图像的大小,问题解决了。

【讨论】:

  • 谢谢!就是这样。我使用 8000 x 8000 图像作为 UIButton 背景图像。用 300 x 300 替换它就可以了。
【解决方案2】:

我敢打赌,延迟和崩溃来自那个同步的downloadImage(checkedUrl) 调用,但只有profiling via Xcode Instruments will tell you for certain

您不仅要依赖一个可能很慢的网络来获取图像在您的主线程,而且您不知道该图像将有多大(如果它是高分辨率图像,并且您已经在其他单元格中加载了许多其他高分辨率 UIImage,您可能会在内存不足的设备上崩溃应用程序。

这不是一个简单的解决方法,我可以输入代码来为您解决它,但我建议做一些更改:

1)

使用异步下载图片的图片缓存

例如SDWebImage

2)

在表格视图中显示更小(且占用内存更少)的缩略图,而不是全尺寸图像。

例如maybe via this technique

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2017-11-22
    • 2014-11-06
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多