在我的应用中,我需要在不同的视图控制器中加载用户详细信息。我加载的详细信息不一定是当前登录的用户(可以从currentUser检索,但有时是其他用户,具体取决于方案。
到目前为止,我发现自己只要满足上述需求,我会做两个问题:首先加载详细信息(例如姓名,电话,地址等),然后加载该用户的个人资料图片。
所以我最终得到了一个嵌套调用,如下所示:
let query = PFQuery(className: "_User")
query.whereKey("appUsername", equalTo: self.friendObject.username!)
query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
let data = results as [PFObject]!
if(error == nil)
{
self.friendObject.name = data[0]["name"] as? String
self.friendObject.profilePic = data[0]["ProfilePic"] as? UIImage
self.nameLabel.text = self.friendObject.name
self.friendObject.objectId = data[0].objectId! as String
self.getProfilePicture(self.friendObject.username!) { (result)->Void in
self.profilePicImageView.image = result
}
}else{
print("Error retrieving user details - try again")
}
}
以下是getProfilePicture函数的定义:
func getProfilePicture(username: String, completion: (result: UIImage) -> Void)
{
var tempImage:UIImage? = UIImage(named: "sample-qr-code.png")!
let query: PFQuery = PFQuery(className: "_User")
query.whereKey("appUsername", equalTo: username)
query.findObjectsInBackgroundWithBlock {
(objects:[PFObject]?, error:NSError?) -> Void in
for object in objects! {
if(object["ProfilePic"] != nil)
{
let imageFiles = object["ProfilePic"] as! PFFile
imageFiles.getDataInBackgroundWithBlock({
(imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
tempImage = UIImage(data:imageData!)!
let temp2Image: UIImage = Toucan(image: tempImage!).resize(CGSize(width: 100, height: 150)).maskWithEllipse(borderWidth: 3, borderColor: UIColor.whiteColor()).image
completion(result: temp2Image)
}
})
}else{
let invalidImage = UIImage(named: "Contacts-100.png")
completion(result: invalidImage!)
}
}
}
}
User parse类中的ProfilePic列属于File类型。我如何优化这一点,以便我只进行一次调用来加载图像和细节(假设请求应尽可能地最小化)。
谢谢,
答案 0 :(得分:0)
将imageView
设为PFImageView
然后您可以设置PFImageView
的相应文件,例如self.friendObject.profilePic = data[0]["ProfilePic"] as? PFFile
,然后您可以self.profilePicImageView.file = self.friendObject.profilePic
然后{ {1}}