0

I have this code to retrieve images from parse data.

How do I query it by creation date? I tried using findObjectsInBackgroundWithBlock but it wont work with a PFFile. What do i do?

Also, as a side question, sometimes when i place the images in a UIImageView they are upside down or sideways. Why?

if let objects = objects as? [PFObject] {
                    for object in objects {

    if let userPicture = object.valueForKey("Image") as? PFFile {
         userPicture.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
         if (error == nil) 
         {
             let image = UIImage(data:imageData!)
             self.ImageArray.insert(image!, atIndex: 0)
         }
         else {
            self.alert("Error: \(error!) \(error!.userInfo!)", Message: "Make sure you have a secure internet connection")        
         }                                        
       dispatch_async(dispatch_get_main_queue()) 
          {
            self.collectionView.reloadData()
            println("Finished Pictures")
          }
       })
     }    
}}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

1 Answers1

0

You have a set of objects which all reference an image file, but probably a set of other things too, and the image can likely be changed. So, the object creation date isn't the same as the image creation date and the image file doesn't know (or at least doesn't expose) it's creation date. You're also currently always adding the images to the start of the array so the position will be set by how big (and therefore how long it takes to download) each image is. Also, trying to download lots of images at the same time could just mean you get lots of timeouts.

So, really you should have a column on your object which holds the date at which the image was updated, and sort the objects by that date. As you download the images you place them into the image array in the same index as the owning object in its array (pad the array out with NSNull so you know what's going on).

Wain
  • 118,658
  • 15
  • 128
  • 151