ios - 使用 Parse 在用户注册时添加个人资料图片 - iOS

标签 ios parse-platform

我正在尝试使用 Parse.com 作为我的后端来创建一个新用户。我能够成功注册用户的信息。但是,我的问题是保存用户的个人资料图片。用户使用相机或照片库选择图片后,我将使用 .xib 文件呈现图像编辑器 ( https://github.com/heitorfr/ios-image-editor )。这一切都没有问题。然后将图像放置在 UIImageView 框中,用户继续填写用户名、电子邮件和密码,然后单击注册。问题是在他们注册并通过 Parse 将所有信息放入表中之后,图像容器似乎是空的。当我单击图像文件时,图片不存在。我似乎无法弄清楚我做错了什么。

查看已加载

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

图像选择器 Controller 委托(delegate)方法

// 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 方法

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

这里是 IBAction 注册码:

- (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 没有任何指示 docs使用 - signUpInBackgroundWithBlock:
时设置除密码和用户名以外的任何信息 您必须先注册用户,然后在 signUp 方法的 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];
            }
        }];
      }
    }];
}

关于ios - 使用 Parse 在用户注册时添加个人资料图片 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999049/

相关文章:

c# - Xamarin iOS Sleep UI线程不起作用

ios - 更改 iOS 日期选择器显示格式

ios - 我需要为每个Apple AppStore提交单独的发行版吗?

ios - 合并解析查询并添加到单独的数组

javascript - 解析云代码 全部保存

javascript - 未捕获 您必须使用 Parse.initialize 指定一个 key

IOS 8 CLLocationManager 问题(授权无效)

ios - 如何在 xcode 代码中显示图像标题而不是图标?

swift - 视频未保存在解析中

ios - Bolts框架[Parse+Facebook]需要使用parse webservice吗?