2015-08-16 137 views
4

我有一個在模擬器中運行良好的應用程序,但是當我在設備上運行應用程序時,該應用程序運行緩慢,偶爾會崩潰。這個問題特別發生在點擊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") 
} 
+0

嘗試逐個刪除約束條件 –

+0

另外,請確保URL處於活動狀態,我去了它的根目錄並顯示它現在沒有提供服務,但可能是因爲他們設置了這種方式來防止黑客攻擊或一些東西。另外,我不確定你的函數「downloadImage(checkedUrl)」是做什麼的,看看這個函數確定這是否是罪魁禍首將是有幫助的 – Loxx

+0

使用Xcode工具名爲「Time Profiler」的工具來檢測你的問題所在在代碼中。 –

回答

0

我敢打賭滯後&崩潰是從同步看downloadImage(checkedUrl)電話來了,但只有profiling via Xcode Instruments will tell you for certain

你不僅取決於潛在的慢速網絡上對你的主線程獲取圖像,但你不知道,圖像有多大將是(如果它是一個高分辨率圖像,並且你已經在其他單元格中加載了許多其他高分辨率UIImage,您可能會在低內存設備上崩潰)。

這不會是一個簡單的修復,我可以只輸入代碼來爲您解決,但我建議做一些改動:

1)

使用的圖像緩存異步下載圖片

eg SDWebImage

2)

顯示小(和較少存儲器密集型)在表格視圖縮略圖和不全尺寸圖像。

例如maybe via this technique

0

我發現罪魁禍首,事實證明它畢竟不在代碼中。我在scrollView中設置了一些背景圖像,分辨率很高(5000x5000)。他們完全咀嚼着內存(在調試器中達到了400mb左右)。我調整了圖像大小,解決了問題。