Eri-Sklii
Eri-Sklii

Reputation: 589

Get data from Parse.com to swift

In my code it receives the images from parse, and show it in a imageView. Here is the code:

http://pastebin.com/kDjAgPRT

If needed, here is my code for upload:

func uploadPost(){
        var imageText = self.imageText.text

        if (imageView.image == nil){
            println("No image 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{
                    //**Success saving, now save image.**//

                    // Create an image data
                    var imageData = UIImagePNGRepresentation(self.imageView.image)
                    // Create a parse file to store in cloud
                    var parseImageFile = PFFile(name: "upload_image2.png", data: imageData)
                    //var parseImageFile = PFFile(data: imageData)
                    posts["imageFile"] = parseImageFile
                    posts.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
                        if error == nil{
                            // Take user home
                            println(success)
                            println("Data uploaded")
                        }
                        else{
                            println(error)
                        }
                    })
                }
                else{
                    println(error)
                }
            })
        }
    }

As you can see, here is my Parse inside "Posts": enter image description here

How can i also get "imageText", "uploader" and "createdAt" for the images? Like instagram has.

Upvotes: 0

Views: 91

Answers (1)

Lamour
Lamour

Reputation: 3030

Try this:

struct Details {
var username:String!
var text:String!
var CreatedAt:NSDate!
var image:UIImage!
init(username:String,text:String,CreatedAt:NSDate,image:UIImage){

    self.username = username
    self.text = text
    self.CreatedAt = CreatedAt
    self.image = image
   }
} 

func QueryImagesFromParse(){

   var arrayOfDetails = [Details]()

var query = PFQuery(className: "Posts")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
    if error == nil
    {
        if let newObjects = objects as? [PFObject] {

            for oneobject in newObjects {
                 var text = oneobject["imageText"] as! String
                 var username = oneobject["uploader"] as! String
                  var time = oneobject.createdAt
                  var userImageFile = oneobject["imageFile"] as! PFFile
                userImageFile.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
                    if error == nil {
                       let newImage = UIImage(data: imageData!)

                         var OneBigObject = Details(username: username, text: text, CreatedAt: time!, image: newImage!)
                          arrayOfDetails.append(OneBigObject)
                         // then reloadData


                    }
                  })

            }

        }
      }
  }
 }

SO NOW with the arrayOfDetails you could populate your cells...

Upvotes: 1

Related Questions