Retrieve photo attached to currentUser / Parse

121 views Asked by At

I am having issues retrieving an imaged attached to the currentUser. Here is the code I am using to save the info to parse.

-(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];

Basically now I'm trying to call the info back to load the users profile. heres what i have so far (not much).

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;
    }

}];
3

There are 3 answers

1
NehaK On

You can use ParseImageView instead of ImageView and set image by following code-

try{
    ParseFile image = ParseUser.getCurrentUser().getParseFile("picture");
    if (image != null) {

        parseImgView.setParseFile(image);
        parseImgView.loadInBackground();
    }
}catch(Exception e)
{
    e.printStackTrace();
}
0
Andrew Cook On

for anyone with the same issue all I had to do to fix the issue was switch my profilePicture class from a UIImageView to a PFImageView. Once that was done I was able to call the file back from parse with this code.

PFUser *currentuser = [PFUser currentUser];
PFFile *pictureFile = [currentuser objectForKey:@"userPictureParse"];
profilePicture.file = pictureFile;
[profilePicture loadInBackground];

Super super simple. Thanks for the assistance guys.

1
NehaK On

May be there is problem in uploading image file to Parse so you have to save your parse-image-file "userProfilePictureFileContainer" also. So change your method by following code-

-(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
        }
    }];
}