小编典典

如何快速转换PFFile到UIImage?

swift

如何快速将PFFile转换为UIImage?

在这段代码中,应用程序正在从解析中获取文件,现在我希望它显示在UIImageView上,但是我得到一个错误…

这是我的代码…

 var ImageArray:UIImage = [UIImage]()
    var textArray:String = [String]()

     var query = PFQuery(className:"ClassName")

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

                if error == nil {


                    if let objects = objects as? [PFObject] {
                        for object in objects {
     //this works fine                       
self.textArray.append(object.valueForKey("Image")! as! String)



    //this does not work...
                            self.ImageArray.append(object.valueForKey("Image")! as! PFFile)
    //I get an error that says PFFile is not convertible to UIImage. How do i convert it?






                        }
                    }
                } else {
                    // Log details of the failure
                    println("Error: \(error!) \(error!.userInfo!)")
                }

阅读 254

收藏
2020-07-07

共1个答案

小编典典

PFFile是任何文件的解析表示。要获取“真实”文件(图像),您需要调用getDataInBackgroundWithBlock。试试吧:

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