从 Parse 下载 PFFile (Image) 将其附加到数组并用该数组 (Swift) 填充 UIImageView [英] Downloading PFFile (Image) from Parse append it to array and fill UIImageView with that array (Swift)

查看:17
本文介绍了从 Parse 下载 PFFile (Image) 将其附加到数组并用该数组 (Swift) 填充 UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析和 swift 方面遇到问题,希望您能帮助我:)

I have a problem with parse and swift and i hope you can help me :)

我正在从 Parse 下载字符串和 PFfile,并希望将其添加到数组中(这工作正常).

I'm downloading strings and PFfiles from Parse and want to add it to an array (this works fine).

然后我想用字符串数组填充一个标签(也可以正常工作)和一个带有 PFFile 数组的 UIImageView,它也是一个图像.

Then I want to fill a Label with the string array (works fine as well) and an UIImageView with the PFFile array which is also an Image.

但我总是得到一个错误:

But I always get an error:

PFFile 不能转换为UIImage"

PFFile is not convertible to "UIImage

但我不知道如何转换此 PFFile 以便可以将其与 UIImage 视图一起使用.

But I have no clue how I can convert this PFFile so that I can use it with the UIImage View.

var titles = [String] ()

var fragenImages = [PFFile] ()

@IBOutlet weak var fragenIm: UIImageView!


@IBOutlet weak var fragenFeld: UILabel!



  override func viewDidAppear(animated: Bool) {


    var query = PFQuery(className:"Post")

    query.whereKey("user", notEqualTo: PFUser.currentUser().username)
    query.limit = 5

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

        if error == nil {



            for object in objects {

         self.titles.append(object["title"] as String)
         self.fragenImages.append(object["imageFile"] as PFFile)


            }



        self.fragenFeld.text = self.titles[0]
        self.fragenIm.image = self.fragenImages[0]





        }

            else {

            println(error)
            }

        }



}

推荐答案

Daniel,图片不是文件,技术上它们是,但您将它们引用为数据而不是 PFObjects 或 PFFiles.因此,基本上获取文件的数据并像往常一样应用:您只是错过了一个简单的步骤:

Daniel, pictures aren't files, technically they are, but you reference them as Data not PFObjects or PFFiles. So, essentially get the Data of the file and apply as you normally would: You just missed a simple step:

for object in objects {
  let userPicture = object["imageFile"] as PFFile
  userPicture.getDataInBackgroundWithBlock({ // This is the part your   overlooking
  (imageData: NSData!, error: NSError!) -> Void in
    if (error == nil) {
      let image = UIImage(data:imageData)
      self.fragenImages.append(image)
    }
  })
}

这篇关于从 Parse 下载 PFFile (Image) 将其附加到数组并用该数组 (Swift) 填充 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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