我在检索附加到currentUser的图像时遇到问题。 这是我用来保存信息解析的代码。
-(IBAction)saveButtonAction:(id)sender {
PFUser *currentUserSave = [PFUser currentUser];
userBioString = userBio.text;
genderFieldString = genderField.text;
ageFieldString = ageField.text;
profilePictureData = UIImageJPEGRepresentation(profilePicture.image, .05f);
PFFile *userProfilePictureFileContainer = [PFFile fileWithData:profilePictureData];
currentUserSave[@"userBioParse"] = userBioString;
currentUserSave[@"userGenderParse"] = genderFieldString;
currentUserSave[@"ageFieldParse"] = ageFieldString;
[currentUserSave setObject:userProfilePictureFileContainer forKey:@"userPictureParse"];
userImageData = UIImageJPEGRepresentation(profilePicture.image, 0.5);
userProfileImageFile = [PFFile fileWithData:userImageData];
[currentUserSave setObject:userProfileImageFile forKey:@"userPictureParse"];
[[PFUser currentUser] saveInBackground];
基本上现在我正在尝试回调信息以加载用户个人资料。 继续我到目前为止(不多)。
PFQuery *queryUser = [PFUser query];
[queryUser whereKey:@"username" equalTo:[[PFUser currentUser]username]];
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error){
PFFile *theImage = [object objectForKey:[PFUser currentUser][@"userPictrueParse"]];
NSData *imageData = [theImage getData];
UIImage *profileImage = [UIImage imageWithData:imageData];
if (profileImage == nil) {
profilePicture.image = [UIImage imageNamed:@"765-default-avatar.png"];
} else {
profilePicture.image = profileImage;
}
}];
答案 0 :(得分:2)
您可以使用ParseImageView代替ImageView,并通过以下代码设置图像 -
try{
ParseFile image = ParseUser.getCurrentUser().getParseFile("picture");
if (image != null) {
parseImgView.setParseFile(image);
parseImgView.loadInBackground();
}
}catch(Exception e)
{
e.printStackTrace();
}
答案 1 :(得分:0)
将图像文件上传到Parse可能存在问题,因此您还必须保存您的parse-image-file“userProfilePictureFileContainer”。因此,请按照以下代码更改您的方法 -
-(IBAction)saveButtonAction:(id)sender {
PFUser *currentUserSave = [PFUser currentUser];
userBioString = userBio.text;
genderFieldString = genderField.text;
ageFieldString = ageField.text;
profilePictureData = UIImageJPEGRepresentation(profilePicture.image, .05f);
PFFile *userProfilePictureFileContainer = [PFFile fileWithData:profilePictureData];
[userProfilePictureFileContainer saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (!error) {
//IMAGE SAVED NOW SET TO USER
currentUserSave[@"userBioParse"] = userBioString;
currentUserSave[@"userGenderParse"] = genderFieldString;
currentUserSave[@"ageFieldParse"] = ageFieldString;
[currentUserSave setObject:userProfilePictureFileContainer forKey:@"userPictureParse"];
userImageData = UIImageJPEGRepresentation(profilePicture.image, 0.5);
userProfileImageFile = [PFFile fileWithData:userImageData];
[currentUserSave setObject:userProfileImageFile forKey:@"userPictureParse"];
[[PFUser currentUser] saveInBackground];
}
else
{
// ERROR OCCURED
}
}];
}
答案 2 :(得分:0)
对于任何有相同问题的人,我必须做的就是解决问题是将我的profilePicture类从UIImageView切换到PFImageView。完成后,我可以使用此代码从解析中调用文件。
PFUser *currentuser = [PFUser currentUser];
PFFile *pictureFile = [currentuser objectForKey:@"userPictureParse"];
profilePicture.file = pictureFile;
[profilePicture loadInBackground];
超级超级简单。谢谢你的帮助。