我有一个数组中的 Parse 用户名列表。 我想获取那些选定用户的图像。 我可以获取当前用户的图像:
if let userPicture = PFUser.currentUser()?["profilePic"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
self.profileImage.image = UIImage(data:imageData!)
}
}
}
我在数组 namesArray
中有用户名列表
最佳答案
尝试以下操作
// Create the PFUser query
var query = PFUser.query()
// Constrain the query to the users contained in the username array
query.whereKey("username", containedIn: namesArray)
// Find the matching users asynchronously
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if (error == nil) {
// Fetch the images of the users
if let userArray = objects as? [PFUser] {
for user in userArray {
if let userPicture = user["profilePic"] as? PFFile {
userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
// Do something with the image data
} else {
// Error handling
}
}
}
}
}
} else {
// Log details of the failure
println("query error: \(error) \(error!.userInfo!)")
}
}
此外,了解您不能在 PFFile 上使用 includeKey
可能很有用。 PFFile 是对 Parse 数据存储中对象的显式引用而不是指针,因此 getDataInBackgroundWithBlock
必须用于每个用户的个人资料图片。
关于ios - 如何从 Swift 2.0 中的解析中获取选定的用户图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723448/