Daniel Keller
Daniel Keller

Reputation: 31

Downloading PFFile (Image) from Parse append it to array and fill UIImageView with that array (Swift)

I have a problem with parse and swift and i hope you can help me :)

I'm downloading strings and PFfiles from Parse and want to add it to an array (this works fine).

Then I want to fill a Label with the string array (works fine as well) and an UIImageView with the PFFile array which is also an Image.

But I always get an error:

PFFile is not convertible to "UIImage

But I have no clue how I can convert this PFFile so that I can use it with the UIImage View.

var titles = [String] ()

var fragenImages = [PFFile] ()

@IBOutlet weak var fragenIm: UIImageView!


@IBOutlet weak var fragenFeld: UILabel!



  override func viewDidAppear(animated: Bool) {


    var query = PFQuery(className:"Post")

    query.whereKey("user", notEqualTo: PFUser.currentUser().username)
    query.limit = 5

    query.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {



            for object in objects {

         self.titles.append(object["title"] as String)
         self.fragenImages.append(object["imageFile"] as PFFile)


            }



        self.fragenFeld.text = self.titles[0]
        self.fragenIm.image = self.fragenImages[0]





        }

            else {

            println(error)
            }

        }



}

Upvotes: 3

Views: 4526

Answers (1)

soulshined
soulshined

Reputation: 10602

Daniel, pictures aren't files, technically they are, but you reference them as Data not PFObjects or PFFiles. So, essentially get the Data of the file and apply as you normally would: You just missed a simple step:

for object in objects {
  let userPicture = object["imageFile"] as PFFile
  userPicture.getDataInBackgroundWithBlock({ // This is the part your   overlooking
  (imageData: NSData!, error: NSError!) -> Void in
    if (error == nil) {
      let image = UIImage(data:imageData)
      self.fragenImages.append(image)
    }
  })
}

Upvotes: 4

Related Questions