我想用查询绘制,我的用户在UIImageView中的个人资料图片...这段代码给了我图片但只有昵称...你能帮我理解错误在哪里吗?
谢谢!
-(void)viewDidAppear:(BOOL)animated {
//Prepare the query to get the profile image
//1
PFQuery *query = [PFQuery queryWithClassName:@"User"];
PFQuery* queryPhoto = [PFQuery queryWithClassName:@"Foto_Profilo"];
//2
PFUser* currentUser = [PFUser currentUser];
if (currentUser) {
[query whereKey:@"user" equalTo:currentUser];
[queryPhoto whereKey:@"Immagine" equalTo:currentUser];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// Verify if there are no errors
if (!error) {
// Retrieve Usename
self.profileViewUsernameLabel.text = [NSString stringWithFormat:@"%@",[[PFUser currentUser]valueForKey:@"username"]] ;
// Retrieve Photo
PFObject* profilePhotoObject = [PFObject objectWithClassName:@"Foto_Profilo"];
PFFile* currentUserPhoto = (PFFile *)[profilePhotoObject objectForKey:@"Immagine"];
UIImageView* currentUserImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:currentUserPhoto.getData]];
[self.showfoto addSubview:currentUserImage];
} else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[errorAlertView show];
}
} ];
}
}
答案 0 :(得分:0)
问题不完全清楚,但我想你得到了名字,而不是图片。
profilePhotoObject
是一个新的空对象,因此当您从中请求objectForKey:@"Immagine"
时,您将获得无效。
您定义queryPhoto
但从未实际执行过它。执行它并将结果与当前块分开处理。
您创建了2个查询。执行1并且不使用结果。你不执行另一个(看起来实际上有图像的那个)。
让我们尝试用以下代码替换您当前的查询:
[queryPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// Verify if there are no errors
if (objects.count >= 1) {
// Retrieve Usename
self.profileViewUsernameLabel.text = [NSString stringWithFormat:@"%@",[[PFUser currentUser]valueForKey:@"username"]] ;
// Retrieve Photo
PFObject* profilePhotoObject = objects[0]
PFFile* currentUserPhoto = (PFFile *)[profilePhotoObject objectForKey:@"Immagine"];
UIImageView* currentUserImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:currentUserPhoto.getData]];
请注意,这会使用您创建的其他查询并使用执行查询的结果。
答案 1 :(得分:0)
使用此...
PFQuery *queryUser = [PFUser query];
[queryUser whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error){
//Cargo la imagen de la celda
PFFile *theImage = [object objectForKey:@"userImage"];
NSData *imageData = [theImage getData];
UIImage *profileImage = [UIImage imageWithData:imageData];
if (profileImage == nil) {
_imgProfile.image = [UIImage imageNamed:@"noPhoto.png"];
} else {
_imgProfile.image = profileImage;
}
}];