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.

2
  • so everything is good when you are saving right ,just want to get them back to your tableView Commented Aug 18, 2015 at 19:29
  • @Lamar - Yes, correct. But not table view, i am using CollectionView. Commented Aug 18, 2015 at 19:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

15 Comments

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: pastebin.com/S5F4RF2M
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
Ok, now there is no errors, but there is no images showing up..?
why not did you retrieve the username and text and date ?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.