如何使用swift将PFFile转换为UIImage? [英] How to convert an PFFile to an UIImage with swift?

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

问题描述

如何使用swift将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...

这是我的代码......

Here is my code...

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆