Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用swift将PFFile转换为UIImage?_Ios_Swift_Parse Platform_Uiimageview_Uiimage - Fatal编程技术网

Ios 如何使用swift将PFFile转换为UIImage?

Ios 如何使用swift将PFFile转换为UIImage?,ios,swift,parse-platform,uiimageview,uiimage,Ios,Swift,Parse Platform,Uiimageview,Uiimage,如何使用swift将PFFile转换为UIImage 在这段代码中,应用程序正在从parse获取文件,现在我希望它显示在UIImageView上,但我得到一个错误 这是我的密码 var ImageArray:UIImage = [UIImage]() var textArray:String = [String]() var query = PFQuery(className:"ClassName") query.findObjectsInBack

如何使用swift将PFFile转换为UIImage

在这段代码中,应用程序正在从parse获取文件,现在我希望它显示在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!)")
                }

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

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)
         }
      })
}
Swift 1.2+版本

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

解析时使用Swift 3。

  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
                    }

                }
            })
        }

我得到一个错误,上面写着“PFData没有成员名称”GetDataInBacGroundithBlock“我得到另一个错误,上面写着“无法调用”GetDataInBacGroundithBlock;使用类型为({(imageData:NSData!,error NSError!)->Void的参数列表,请在将其作为答案提交之前进行测试。请在提问之前进行谷歌搜索:)