iOS应用程序在Xcode模拟器中运行良好,但在设备上运行缓慢并且崩溃
问题描述:
我有一个在模拟器中运行良好的应用程序,但是当我在设备上运行应用程序时,该应用程序运行缓慢,偶尔会崩溃。这个问题特别发生在点击tableview单元格并转换到新视图时。当点击一个单元格时会有一两个延迟,那么新的视图将以缓慢的方式推迟。这也是偶尔会崩溃的地方。iOS应用程序在Xcode模拟器中运行良好,但在设备上运行缓慢并且崩溃
该应用在设备上运行良好,直到最近在代码中添加了一些更改,这是我认为问题出在哪里。我添加了包含可疑代码的整个viewDidLoad波纹管(代码中指示)。
我注意到的另外一件事情是,调试导航器中的CPU使用率约为130%,而应用程序在设备上发生问题的位置运行该应用程序。
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")
}
答
我敢打赌滞后&崩溃是从同步看downloadImage(checkedUrl)
电话来了,但只有profiling via Xcode Instruments will tell you for certain。
你不仅取决于潜在的慢速网络上对你的主线程获取图像,但你不知道,图像有多大将是(如果它是一个高分辨率图像,并且你已经在其他单元格中加载了许多其他高分辨率UIImage,您可能会在低内存设备上崩溃)。
这不会是一个简单的修复,我可以只输入代码来为您解决,但我建议做一些改动:
1)
使用的图像缓存异步下载图片
eg SDWebImage
2)
显示小(和较少存储器密集型)在表格视图缩略图和不全尺寸图像。
答
我发现罪魁祸首,事实证明它毕竟不在代码中。我在scrollView中设置了一些背景图像,分辨率很高(5000x5000)。他们完全咀嚼着内存(在调试器中达到了400mb左右)。我调整了图像大小,解决了问题。
尝试逐个删除约束条件 –
另外,请确保URL处于活动状态,我去了它的根目录并显示它现在没有提供服务,但可能是因为他们设置了这种方式来防止黑客攻击或一些东西。另外,我不确定你的函数“downloadImage(checkedUrl)”是做什么的,看看这个函数确定这是否是罪魁祸首将是有帮助的 – Loxx
使用Xcode工具名为“Time Profiler”的工具来检测你的问题所在在代码中。 –