我正在尝试使用Parse.com作为我的后端创建一个新用户。我能够成功注册用户的信息。但是,我的问题是保存用户的个人资料图片连同它。在用户使用相机或照片库选择图片之后,我将使用.xib文件呈现图像编辑器(https://github.com/heitorfr/ios-image-editor)。这一切都很好,没有问题。然后图像被放置在UIImageView框中,用户继续填写用户名、电子邮件和密码,然后点击注册。问题是他们注册后,所有的信息通过解析放置在表中,图像容器似乎是空的。当我点击图像文件时,图片不在那里。我似乎不知道我做错了什么。
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;
}
图像选择器控制器委托方法
// 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]; }
<<p> UIActionSheetDelegate方法/strong> -(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];
}
}];
}
}
在使用- signUpInBackgroundWithBlock:
时,没有迹象表明Parse.com API文档中设置了密码和用户名以外的任何信息您必须首先注册用户,然后在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];
}
}];
}
}];
}
您需要将其设置为将文件发送到数据库的位置,因为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替换为图像视图的任何名称。它将把上传的图像设置为这个视图。