2015-06-30 30 views
0

我正在使用解析来上传和检索图像。我正在使用下面的代码来上传图片。只检索用户上传的图像

@IBAction func uploadButton(sender: AnyObject) { 
     var imageText = uploadMessage.text 

     if uploadPreviewImageView.image == nil { 
      //image is not included alert user 
      println("Image not uploaded") 
     }else { 

      var posts = PFObject(className: "Posts") 
      posts["imageText"] = imageText 
      posts["uploader"] = PFUser.currentUser() 
      posts.saveInBackgroundWithBlock({ 
       (success: Bool, error: NSError?) -> Void in 

       if error == nil { 

        var imageData = UIImagePNGRepresentation(self.uploadPreviewImageView.image) 
        var parseImageFile = PFFile(name: "uploaded_image.png", data: imageData) 
        posts["imageFile"] = parseImageFile 
        posts.saveInBackgroundWithBlock({ 
         (success: Bool, error: NSError?) -> Void in 

         if error == nil { 

正如问题的标题所述,我只想检索用户已拍摄的图像。

var query = PFQuery(className: "Tops") 
query.orderByDescending("createdAt") 
query.findObjectsInBackgroundWithBlock{ 
    (posts: [AnyObject]?, error: NSError?) -> Void in 
    if error == nil { 
     for post in posts!{ 
      self.imageFiles.append(post["imageFile"]as! PFFile) 
      self.imageText.append(post["imageText"]as! String) 
    } 


    println(self.imageFiles.count) 
     self.topsTableView.reloadData() 

} else { 
     println(error) 
}}} 

在上面的代码我可以然而,检索图像,如果我注销并登录我能看到其他用户上传的图片另一个帐户。这怎么可能?我无法找到这样做的教程和文档。 谢谢

回答

1

你基本上需要为你的查询定义一个谓词。解析支持NSPredicate和他们自己的谓词式机制。

出于您的目的,您应该将查询条件设置为仅用户拍摄的图片。这可以表示为query.whereKey("uploader", equalTo: PFUser.currentUser())

用简单的英语,这就像说 - 嘿查询,返回结果上传者属性有我的当前用户的值。

你已经设置此属性为你上传它:]

+0

谢谢你的回答!我有点困惑。因此,我们在检索代码中编写'query.whereKey(“uploader”,equalTo:PFUser.currentUser()'?是否有任何文档或实现方法可以看到? – Devnew

+0

是的,在初始化' PFQuery()'。您可以在查询部分的parse.com iOS指南中阅读类似的示例。 –