itzjshine
itzjshine

Reputation: 1

Adding profile picture at user signup using Parse - iOS

I'm trying to create a New User using Parse.com as my backend. I was able to successfully register the user's info. But, my problem is saving the user's profile picture along with it. After the user selects the picture using the camera or photo library, I am presenting an image editor (https://github.com/heitorfr/ios-image-editor) using a .xib file. This all works fine with no problems. Then the image is placed in the UIImageView box and the user proceeds to fill in the Username, email and password and they click signup. The problem is after they sign up and all the info gets placed in the table via Parse, the image container seems to be empty. When I click on the image file, the picture is not there. I can't seem to figure out what I'm doing wrong.

View Did Load

- (void)viewDidLoad {
[super viewDidLoad];

self.addPhotoImageView.contentMode = UIViewContentModeScaleAspectFill;


// Create add photo popup
self.addPhotoActionSheet = [[UIActionSheet alloc] initWithTitle:@"Select source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take photo", @"Choose photo", nil];

// Create image picker
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.editing = YES;
}

Image Picker Controller Delegate Methods

// Switches to the image editing view
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];

if (self.imageEditorViewController == nil) {

    self.imageEditorViewController = [[HFImageEditorViewController alloc] initWithNibName:@"ImageEditor" bundle:nil]; }

self.imageEditorViewController.sourceImage = image;
self.imageEditorViewController.rotateEnabled = NO;
self.imageEditorViewController.checkBounds = YES;
[self.imageEditorViewController reset:NO];


__weak typeof(self) weakSelf = self;
self.imageEditorViewController.doneCallback = ^(UIImage *editedImage, BOOL canceled) {

    if (!canceled) {

        [weakSelf.addPhotoImageView setImage:editedImage];


    }
    // Hide editor
    [weakSelf.imageEditorViewController dismissViewControllerAnimated:YES completion:nil];
};

[self presentViewController:self.imageEditorViewController animated:NO completion:nil]; }

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[self dismissViewControllerAnimated:YES completion:nil]; }

UIActionSheetDelegate methods

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
    case 0:
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        break;

    case 1:
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        break;

    default:
        return;
}

[self presentViewController:self.imagePicker animated:YES completion:nil]; }

Here is IBAction signup code:

- (IBAction)signup:(id)sender {

NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 0.5f);
PFFile *imageFile = [PFFile fileWithName:@"Profileimage.jpg" data:imageData];

NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([username length] == 0 || [email length] == 0 || [password length] == 0) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops" message:@"Make sure you fill in all the fields!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertView show];
}
else {
    PFUser *newUser = [PFUser user];
    newUser[@"ProfilePic"] = imageFile;
    newUser.username = username;
    newUser.email = email;
    newUser.password = password;

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alertView show];
        }
        else {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }];

}

}

Upvotes: 0

Views: 2037

Answers (2)

Kishon Daniels
Kishon Daniels

Reputation: 55

You need to set it up to where you are sending a file to the database, since Parse will store images as such. Here's how I save images in my projects.

.h file:

        @property (nonatomic, weak) IBOutlet UITextField *username
        @property (nonatomic, weak) IBOutlet UITextField *email;
        @property (nonatomic, strong) IBOutlet UIImageView *photo;
        @property (nonatomic, strong) IBOutlet UIButton *addUserPhoto;

.m file in your save method

        NSString *username = _username.text;
        NSString *email = _email.text;

        PFObject *newUser = [PFObject objectWithClassName:@"User"];
        [newUser setObject:username forKey:@"username"];
        [newUser setObject:email forKey:@"email"];

 // image
        NSData *imageData = UIImageJPEGRepresentation(_photo.image, 0.8);
        NSString *filename = [NSString stringWithFormat:@"%@.png", _name.text];
        PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
        [newUser setObject:imageFile forKey:@"profileImage"];

Replace _Photo with whatever you named the image view. It will set the uploaded image to this view.

Upvotes: 1

race_carr
race_carr

Reputation: 1377

There's no indication from the Parse.com API docs that any information other than password and username are set when using - signUpInBackgroundWithBlock:
You will have to sign up the user first, then have a nested call within the PFBooleanResultBlock of the signUp method to perform

else {
    PFUser *newUser = [PFUser user];
    newUser.username = username;
    newUser.email = email;
    newUser.password = password;

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      if (error) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];
      }
      else {
        newUser[@"ProfilePic"] = imageFile;
        [newUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (! error) {         
              [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
      }
    }];
}

Upvotes: 1

Related Questions