使用Parse-iOS在用户注册时添加配置文件图片

使用Parse-iOS在用户注册时添加配置文件图片,ios,parse-platform,Ios,Parse Platform,我正在尝试使用Parse.com作为我的后端创建一个新用户。我能够成功注册用户的信息。但是,我的问题是保存用户的个人资料图片。在用户使用相机或照片库选择图片后,我将使用.xib文件显示一个图像编辑器()。这一切都很好,没有问题。然后图像被放置在UIImageView框中,用户继续填写用户名、电子邮件和密码,然后单击注册。问题是,在他们注册并通过解析将所有信息放入表中之后,图像容器似乎是空的。当我点击图像文件时,图片不在那里。我似乎不知道我做错了什么 视图未加载 - (void)viewDidLo

我正在尝试使用Parse.com作为我的后端创建一个新用户。我能够成功注册用户的信息。但是,我的问题是保存用户的个人资料图片。在用户使用相机或照片库选择图片后,我将使用.xib文件显示一个图像编辑器()。这一切都很好,没有问题。然后图像被放置在UIImageView框中,用户继续填写用户名、电子邮件和密码,然后单击注册。问题是,在他们注册并通过解析将所有信息放入表中之后,图像容器似乎是空的。当我点击图像文件时,图片不在那里。我似乎不知道我做错了什么

视图未加载

- (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;
}
图像选择器控制器委托方法

// 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]; }
-(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]; }
UIActionSheetDelegate方法

// 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]; }
-(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]; }
以下是iAction注册代码:

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

}

}Parse.com API没有显示在使用
-signUpInBackgroundWithBlock:

您必须首先注册用户,然后在注册方法的
PFBooleanResultBlock
中进行嵌套调用

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

您需要将其设置为将文件发送到数据库的位置,因为Parse将存储图像。下面是我在项目中保存图像的方法

.h文件:

        @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文件

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

将_Photo替换为您命名的图像视图。它会将上载的图像设置到此视图。

您是否保存过图像或用户?你有自定义的用户类吗?我不认为我需要创建一个用户类,因为我正在使用Parse?谢谢你的快速回复。我尝试使用saveinbackgroundithblock,结果也一样。它将容器保存在Parse中,并显示那里应该有一个图像,但当我单击查看图像时,它是空的。您是否尝试过调试PFFile创建代码是否有问题?比如PFFile的NSData可能没有正确创建。这不要紧,但您也可以尝试
[newUser setObject:imageFile forKey:@“ProfilePic]”我还没有试过调试它,因为它们不应该有任何问题。在使用相同的方法之前,我创建了一个项目,效果很好。就在这次,我通过imageEditor xib文件传递它。也许这与此有关。我会把密码寄出去。你用Swift怎么做?