我有一個在模擬器中運行良好的應用程序,但是當我在設備上運行應用程序時,該應用程序運行緩慢,偶爾會崩潰。這個問題特別發生在點擊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")
}
嘗試逐個刪除約束條件 –
另外,請確保URL處於活動狀態,我去了它的根目錄並顯示它現在沒有提供服務,但可能是因爲他們設置了這種方式來防止黑客攻擊或一些東西。另外,我不確定你的函數「downloadImage(checkedUrl)」是做什麼的,看看這個函數確定這是否是罪魁禍首將是有幫助的 – Loxx
使用Xcode工具名爲「Time Profiler」的工具來檢測你的問題所在在代碼中。 –