Loading PFFile (Image) from Parse adds it to the array and populates the UIImageView with that array (Swift)

I have a syntax and swift problem and I hope you can help me :)

I am loading strings and PF files from Parse and want to add them to an array (this works great).

Then I want to populate the Label with a string array (works great) and a UIImageView with a PFFile array which is also an image.

But I always get the error:

PFFile does not convert to "UIImage

But I don't know 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)
            }

        }



}

      

+3


source to share


1 answer


Daniel, pictures are not files, technically they are, but you refer to them as Data not PFObjects or PFFiles. So essentially get the file data and apply as usual: 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)
})

      

+4


source







All Articles