繁体   English   中英

iOS应用程序在Xcode模拟器中运行良好,但速度很慢并且在设备上崩溃

[英]iOS app runs fine in Xcode simulator, but is slow and crashes on a device

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

该应用程序在设备上正常运行,直到最近添加了一些代码更改,这是我认为问题所在。 我添加了包含可疑代码的整个viewDidLoad bellow(在代码中指出)。

我注意到的另一件事是调试导航器中的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")
}

我找到了罪魁祸首,事实证明它毕竟不在代码中。 我在scrollView中设置了一些背景图像,分辨率很高(5000x5000)。 他们绝对是在咀嚼内存(在调试器中达到400mb左右)。 我调整了图像大小,问题解决了。

我敢打赌,滞后和崩溃来自同步看起来的downloadImage(checkedUrl)调用,但只有通过Xcode Instruments进行分析才能确定

您不仅依赖于可能较慢的网络来获取主线程上的图像,而且您不知道图像的大小(如果它是高分辨率图像,并且您已经拥有许多其他高图像在其他单元格中加载的UIImages,您可能会从低内存设备上的应用程序中崩溃)。

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

1)

使用可以异步下载图片的图像缓存

例如SDWebImage

2)

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

例如, 可能通过这种技术

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM