我真的很难尝试从Parse加载图像。。。我已经设置好了,它通过JavaScript将图像作为文件发送到Parse(参见屏幕截图),现在我正试图在Swift 2.1/Xcode 7.2beta中将图像加载到我的UIImageView中。没有错误返回,应用程序的其余部分按预期运行。不过,我似乎不能让它工作。任何帮助都将不胜感激。
let currentUser = PFUser.currentUser()
if currentUser != nil {
if let companyImage = currentUser!["pic"] as? PFFile {
companyImage.getDataInBackgroundWithBlock({ ( imageData: NSData?, error: NSError?) -> Void in
if error == nil{
self.pic.image = UIImage(data: imageData!)
} else {
print("No image found")
}
})
}
Here's a screenshot of what shows up in Parse after I save it through a website I created with JavaScript
编辑:
我还在我的应用程序中编辑了Info.plist文件,以确保可以处理对internet的请求,并且它工作了,因为我能够加载用户的用户名。
最佳答案
我认为当前用户不包含默认对象以外的其他对象。请确保您拥有该行的用户数据。
所以试试这个
var query = PFUser.query()
query.whereKey("objectId", equalTo: PFUser.currentUser().objectId)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
if let companyImage = objects[0]!["pic"] as? PFFile {
companyImage.getDataInBackgroundWithBlock({ ( imageData: NSData?, error: NSError?) -> Void in
if error == nil{
self.pic.image = UIImage(data: imageData!)
} else {
print("No image found")
}
})
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}