2015-10-17 132 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