0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Eri-Sklii
  • 549
  • 4
  • 10
  • 21

1 Answers1

1

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...

Lamour
  • 3,002
  • 2
  • 16
  • 28
  • Hm, when i run this it runs without error, but i get Thread error: "Could not cast value of type 'PFUser' (0x1001852b0) to 'NSString' (0x195d99768).", on line: "var username = oneobject["uploader"] as! String". Updated code: http://pastebin.com/S5F4RF2M – Eri-Sklii Aug 18 '15 at 19:56
  • I see what is going on so you know what you should create a column with the username and called uploader into parse ... then in your upload code you should save the **currentUser.username**.... this is an easy way out you don't have to deal with Pointer – Lamour Aug 18 '15 at 19:59
  • Ok, now there is no errors, but there is no images showing up..? – Eri-Sklii Aug 18 '15 at 20:09
  • why not did you retrieve the username and text and date ? – Lamour Aug 18 '15 at 20:10
  • Starting from line 86 ... you should not use that array because your data are inside of ** arrayOfDetails** , And I assume that your custom cell has three UILablel and one imageView .. so **var post = arrayOfDetails[indexPath.row]** then **cell.label.text = post.username** so on – Lamour Aug 18 '15 at 20:18
  • What exactly did you mean now?: http://s10.postimg.org/69xqalj7t/Screen_Shot_2015_08_18_at_22_30_23.png – Eri-Sklii Aug 18 '15 at 20:31
  • you have to make that changes into this function **func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {** – Lamour Aug 18 '15 at 20:32
  • Yes, i tried that aswell, and still get this: http://s8.postimg.org/m1k8z5k1x/Screen_Shot_2015_08_18_at_22_34_23.png – Eri-Sklii Aug 18 '15 at 20:35
  • Error: http://s11.postimg.org/lg6azvewz/Screen_Shot_2015_08_18_at_22_48_09.png, should i change line to: "cell.uploadedTimeLabel.text = post.CreatedAt.timeIntervalSinceNow.description" ? – Eri-Sklii Aug 18 '15 at 20:52
  • YES, it worked! Thanks :) Only missing now is the time and to make that the newest uploaded images comes to the top. – Eri-Sklii Aug 18 '15 at 21:53
  • i think you should try and if anything you could ask and don't forget to vote his answer if it helps you – Lamour Aug 18 '15 at 21:55
  • I have been trying, thats why i didn't answer so fast. I don´t understand how to to it. – Eri-Sklii Aug 18 '15 at 22:01
  • you have problem with the date – Lamour Aug 18 '15 at 22:18
  • what did you try ? what you have to do is to convert the **post.CreatedAt** to a string – Lamour Aug 18 '15 at 22:32
  • http://stackoverflow.com/questions/2927028/how-do-i-get-hour-and-minutes-from-nsdate check i have to go but don't forget to vote up that question – Lamour Aug 18 '15 at 22:45