本文介绍了如何快速将 PFFile 转换为 UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何快速将 PFFile 转换为 UIImage?

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

在这段代码中,应用程序正在从解析中获取文件,现在我希望它显示在 UIImageView 上,但出现错误...

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 error...

这是我的代码...

 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 是任何类型文件的解析表示.要获取真实"文件(图像),您需要调用 getDataInBackgroundWithBlock.试试看:

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

这篇关于如何快速将 PFFile 转换为 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 21:06