How to convert an PFFile to an UIImage with swift?

后端 未结 3 1526
难免孤独
难免孤独 2020-12-04 00:43

How do i convert an PFFile to an UIImage with swift?

In this code, the app is getting the file from parse and now i want it to show on a UIImageView, But i get an er

相关标签:
3条回答
  • 2020-12-04 00:49

    PFFile is a parse representation of anykind of file. To get the "real" file (image), you need to call getDataInBackgroundWithBlock. Try it:

    if let userPicture = object.valueForKey("Image")! as! PFFile {
       userPicture.getDataInBackgroundWithBlock({
          (imageData: NSData!, error NSError!) -> Void in
             if (error == nil) {
                let image = UIImage(data:imageData)
                self.ImageArray.append(image)
             }
          })
    }
    
    0 讨论(0)
  • 2020-12-04 00:50

    With Swift 3 on Parse.

      if let photo = obj.file as? PFFile {
           photo.getDataInBackground(block: {
              PFDataResultBlock in
              if PFDataResultBlock.1 == nil {//PFDataResultBlock.1 is Error
                        if let image = UIImage(data:PFDataResultBlock.0!){
                            //PFDataResultBlock.0 is Data
                            photoCell.attachedPicture.image = image
                        }
    
                    }
                })
            }
    
    0 讨论(0)
  • 2020-12-04 00:58

    Swift 1.2+ Version

            if let userPicture = PFUser.currentUser()!.valueForKey("Image") as? PFFile {
                userPicture.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
    
                    if error == nil {
                        let image = UIImage(data:imageData!)
                        self.ImageArray.append(image)
                    }else{
                        print("Error: \(error)")
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题