我试图将我在Buddy For Parse中存储的图像显示为UIImageView,但是我一直收到此错误:
无法将'PFFileObject'(0x1045e0568)类型的值转换为'NSString'(0x1041d75d8)。 2019-04-13 18:15:09.869460-0500 PerfectLaptop [43839:3094232]无法将'PFFileObject'(0x1045e0568)类型的值转换为'NSString'(0x1041d75d8)。
我已经将许多字符串存储到Parse中,并且能够毫无问题地访问它们,并且还存储了我想要使用的图像,但无论我尝试什么,我似乎都无法使其工作。我发现的许多解决方案都包括将对象转换为PFFile
,但这似乎不再存在。
let query = PFQuery(className: "whichOneRecommended")
query.findObjectsInBackground { (objects, error) in
if error == nil
{
if let returnedobjects = objects
{
for object in returnedobjects
{
if (object["whichOne"] as! String).contains("\(self.whichOneJoined)")
{
self.laptopImage.image = UIImage(named: (object["laptopImage"]) as! String)
}
}
}
}
}
虽然图像文件是可查看的并且可以在解析中下载,但我似乎无法将它显示在imageview
中,并且我希望通过运行此函数以编程方式更改图像视图,就像我对对象的其他部分一样。
提前致谢
首先要注意的是PFFile
已经改名为PFFileObject
。
你试图将object["laptopImage"]
这个Any
类型的值传递给UIImage(named:)
,这是无法完成的,因为该函数需要String
。
首先,您需要创建PFFileObject
类型的常量:
let file = object["laptopImage"] as? PFFileObject
然后下载文件数据,从PFFileObject
创建一个UIImage并将图像分配给UIImageView
:
file.getDataInBackground { (imageData: Data?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let imageData = imageData {
let image = UIImage(data: imageData)
self.laptopImage.image = image
}
}
有关这方面的详细信息,请参阅section on files in the iOS Guide。