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

女生的网名这么多〃 提交于 2019-12-17 21:08:58

问题


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


来源:https://stackoverflow.com/questions/27595220/downloading-pffile-image-from-parse-append-it-to-array-and-fill-uiimageview-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!