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

时间:2015-08-16 02:35:30

标签: ios performance swift uitableview crash

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

该应用程序在设备上正常运行,直到最近添加了一些代码更改,这是我认为问题所在。我已经添加了包含可疑代码的整个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")
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我打赌滞后&崩溃来自同步查看downloadImage(checkedUrl)调用,但只有profiling via Xcode Instruments will tell you for certain

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

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

1)

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

e.g。 SDWebImage

2)

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

e.g。 maybe via this technique