-->

Downloading PFFile (Image) from Parse appe

2019-01-20 12:46发布

问题:

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)
            }

        }



}

回答1:

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)
})