2015-10-17 174 views
0

如何檢查圖像中是否存在解析?檢查圖像是否存在Parse.com

這是我的查詢代碼:

func queryFromParse(){ 
     self.imageFiles.removeAll() 
     var query = PFQuery(className: "currentUploads") 
     query.orderByDescending("createdAt") 
     query.findObjectsInBackgroundWithBlock { (posts: [AnyObject]?, error: NSError?) -> Void in 
      if (error == nil){ 
       // Success fetching objects 
       for post in posts!{ 
        println(post) 
        self.imageFiles.append(post["imageFile"] as! PFFile) 
        self.imageText.append(post["imageText"] as! String) 
       } 
       self.collectionView.reloadData() 
       println(self.imageFiles.count) 

      } 
      else{ 
       println(error) 
      } 
     } 
    } 

如果圖像無法上傳來分析,它看起來像這樣: Image Here

應用程序崩潰,如果不包含圖像,因此如何我可以檢查圖像是否存在,並跳過它,如果它不包含圖像?

+0

您可以通過使用PFQuery來使用query.whereKeyExists(「keyName」)。 –

回答

0

我認爲你的代碼崩潰了,因爲你在塊中使用強制轉型(post [「imageFile」]!PFFile和post [「imageText」]作爲!String)。但是在你的代碼中還有另一個問題。 self.collectionView.reloadData()是一個UI代碼,所以它需要在主隊列上完成。 findObjectsInBackgroundWithBlock方法在另一個線程中運行該塊,以便在函數已經返回後的一段時間後執行閉包。如果你在閉包中有一個UI相關的東西,你可以使用dispatch_async來獲得主隊列。

if let unwrappedPosts = posts as? [PFObject] { 
    for post in unwrappedPosts{ 
     println(post) 
     if let file = post["imageFile"] as? PFFile {self.imageFiles.append(file) } 
     if let text = post["imageText"] as? String { self.imageText.append(text) } //the above to if lets are to be extremely safe (probably not necessary) 
    } 
    dispatch_async(dispatch_get_main_queue(), { 
     self.collectionView.reloadData() 
     print(self.imageFiles.count)   
    }) 
} else { print("found nil") } 
+0

是的,謝謝!你可以快速看看這個:http://pastebin.com/BwsZjwU6 - 我試圖讓「cell.imageTimeLabel.text」如「1分鐘前」,「5分鐘前」等等。但是當我運行應用程序時,它崩潰了。什麼可能是錯誤的? – IdaEmilie

+0

這是很多的代碼,它需要時間來通過你的項目,所以我會使用斷點來找出它崩潰的地方,並在這裏發佈代碼 – Lukas

+0

在我的'cellForItemAtIndexPath'它崩潰在這裏:'cell.imageTimeLabel.text = self .imageCreated [indexPath.row]' - 任何想法? – IdaEmilie