ios - 如何从 Swift 2.0 中的解析中获取选定的用户图像

标签 ios parse-platform swift2

我有一个数组中的 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/

相关文章:

ios - 比较和匹配数组值

c++ - 在 iOS 中启用异常

影响应用下载的 iOS 版本

ios - 分析语音模式IOS

ios - iOS 7中UISearchDisplayController的全屏背景拦截触摸事件

swift - 无法在 Swift 2.3 中编译 Siri 扩展

swift - 如何在PFImageView中显示使用云代码上传的图片

ios - 重新加载 TableView 不显示所有对象

swift - Swift 中获取 UIPanGestureRecognizer 的起点和终点

python - Swift 等效于 Python 切片赋值