iOS应用程序在Xcode模拟器中运行良好,但速度较慢,并且在设备上崩溃
我有一个应用程序在模拟器中运行良好,但是当我在设备上运行它时,该应用程序运行缓慢,偶尔会崩溃。当点击tableview单元格并转换到新视图时,会出现此问题。当点击一个单元格时,有一到两秒的延迟,那么新视图将以缓慢的延迟方式推送。这也是它偶尔会崩溃的地方 该应用程序在设备上运行良好,直到最近对代码进行了一些更改,我认为这就是问题所在。我已经添加了整个viewDidLoad bellow,其中包含可疑代码(在代码中指示) 我注意到的另一件事是,当应用程序在sim卡中运行时,debug navigator中的CPU使用率达到130%左右,而问题可能会在设备上发生iOS应用程序在Xcode模拟器中运行良好,但速度较慢,并且在设备上崩溃,ios,performance,swift,uitableview,crash,Ios,Performance,Swift,Uitableview,Crash,我有一个应用程序在模拟器中运行良好,但是当我在设备上运行它时,该应用程序运行缓慢,偶尔会崩溃。当点击tableview单元格并转换到新视图时,会出现此问题。当点击一个单元格时,有一到两秒的延迟,那么新视图将以缓慢的延迟方式推送。这也是它偶尔会崩溃的地方 该应用程序在设备上运行良好,直到最近对代码进行了一些更改,我认为这就是问题所在。我已经添加了整个viewDidLoad bellow,其中包含可疑代码(在代码中指示) 我注意到的另一件事是,当应用程序在sim卡中运行时,debug navigat
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")
}
我打赌延迟和崩溃来自于同步的
下载映像(checkedUrl)
调用,但仅限于此
你不仅依赖一个速度可能较慢的网络在主线程上获取图像,而且你不知道该图像会有多大(如果它是一个高分辨率图像,并且你已经在其他单元格中加载了许多其他高分辨率图像,你很可能会在内存不足的设备上崩溃)
这不是一个简单的修复,我可以直接键入代码为您解决问题,但我建议做一些更改:
(一)
使用异步下载图片的图像缓存
e、 g
(二)
在表视图中显示更小(内存占用更少)的缩略图,而不是全尺寸图像
e、 g.我找到了罪犯,结果证明它根本不在代码中。我在scrollView中设置了一些高分辨率(5000x5000)的背景图像。他们完全占用了内存(调试器中的内存达到了400mb左右)。我调整了图像的大小,问题解决了。尝试逐个删除约束。另外,确保URL处于活动状态,我找到了它的根目录,显示它现在没有服务,但可能是他们设置它以防止黑客攻击或其他事情。另外,我不确定你的函数“downloadImage(checkedUrl)”是做什么的,看看这个函数是做什么来确定这是否是罪魁祸首会很有帮助。使用名为“时间分析器”的Xcode工具来检测你在代码中遇到的问题。谢谢!就这样。我使用8000 x 8000的图像作为背景图像。用300 x 300替换它就成功了。