我是Firebase的新手,我一直关注他们的文档here。 但是,我尝试过的任何东西似乎都没有用。
我想做什么:
1)注册用户 -Works
2)让用户在注册过程中选择个人资料图片 - 没有工作。
代码:
- (void) registerNewUser:(FIRUser *)user
{
FIRUser *currentUser = [FIRAuth auth].currentUser;
NSString *email = emailAddressTxtField.text;
NSString *password = passwordTxtField.text;
NSString *username = usernameTxtField.text;
[[FIRAuth auth]
createUserWithEmail:email
password:password
completion:^(FIRUser *_Nullable user,
NSError *_Nullable error)
{
if (error)
{
NSLog(@"%@", error.localizedDescription);
return;
}
else
{
////ASSIGN NEW USER THEIR NAME////
self.databaseRef = [[FIRDatabase database] reference];
[[[databaseRef child:@"users"] child:user.uid]
setValue:@{@"name": username}];
}
}];
////ESTABLISHED A USER, SO LET'S ASSIGN THEIR PIC TO THEM////
if (profilePicImageView.image)
{
FIRStorageReference *profilePicRef = [[storageRef child:@"images/profilePicture.jpg"] child:currentUser.uid];
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";
NSData *imageData = UIImageJPEGRepresentation(self.profilePicImageView.image, 0.8);
NSLog(@"metadata from image: %@", metadata);
[profilePicRef putData:imageData metadata:metadata completion:^(FIRStorageMetadata *metadata, NSError *error)
{
if (error != nil)
{
NSString *profileImageURL = metadata.downloadURL.absoluteString;
NSLog(@"Profile Image URL from image: %@", profileImageURL);
[ProgressHUD showSuccess:[NSString stringWithFormat:@"%@ Successfully Registered!!", username]];
[self.segmentedLoginRegister setSelectedSegmentIndex:0];
[self checkSegmentedControl];
[ProgressHUD showSuccess:[NSString stringWithFormat:@"Welcome %@!", username]];
}
else
{
NSLog(@"Failed to Register User with profile image");
}
}];
}
}
其他信息:
照片仅来自用户设备的相机胶卷 调试区域打印:
[Generic]创建具有未知类型的图像格式是错误
答案 0 :(得分:1)
我的猜测是profilePicImageView.image实际上并没有包含有效的图像对象。
答案 1 :(得分:1)
如何注册用户
假设您有 3个文本字段来注册用户。您有 usernameTextfield,emailTextField和passwordTextfield 。我们还希望与此用户关联 profilePicture 。所以我们先:
建立了一种方法,将我们的用户值保存到Firebase数据库:
-(void) saveValuesForUser:(FIRUser *) user
{
NSString *username = usernameTxtField.text;
self.databaseRef = [[FIRDatabase database] reference];
[[[databaseRef child:@"users"] child:user.uid]
setValue:@{@"Name": username, @"Profile Picture": profileImageURL}];
}
上面的profileImageURL是一个NSString,您可以在视图控制器的实现之上创建,但我们也应该创建一个我们可以在整个ViewController中访问的UIImage:
#import "ViewController.h"
@import Photos;
@interface ViewController ()
@end
NSString *profileImageURL;
UIImage *profileImage;
@implementation ViewController
我猜你知道如何从UIButton启动相机,但如果你不这样做,让我们调用这些方法,以便当我们的用户点击按钮选择他们的图像时,它被设置为我们的profilePicture .imageView:
- (IBAction)chooseImageAction:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
picker.delegate = self;
profileImage = info[UIImagePickerControllerEditedImage];
profilePicImageView.image = profileImage;
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
现在我们正在用火烹饪!现在我们已经拥有了用户想要为其profilePicture上传的图像,让我们创建一种方法将其作为NSURL保存到我们的Firebase数据库中:
- (void) saveProfileImage
{
FIRUser *currentUser = [FIRAuth auth].currentUser;
NSString *username = usernameTxtField.text;
if (profilePicImageView.image != nil)
{
FIRStorage *storage = [FIRStorage storage];
storageRef = [storage referenceForURL:@"gs://PUTYOURFIREBASEHERE.appspot.com"];
NSString *imageID = [[NSUUID UUID] UUIDString];
NSString *imageName = [NSString stringWithFormat:@"Profile Pictures/%@.jpg",imageID];
FIRStorageReference *profilePicRef = [storageRef child:imageName];
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";
NSData *imageData = UIImageJPEGRepresentation(self.profilePicImageView.image, 0.8);
[profilePicRef putData:imageData metadata:metadata completion:^(FIRStorageMetadata *metadata, NSError *error)
{
if (!error)
{
profileImageURL = metadata.downloadURL.absoluteString;
[self saveValuesForUser: currentUser];
}
else if (error)
{
NSLog(@"Failed to Register User with profile image");
}
}];
}
}
重要提示:请确保为storageRef的值插入gs://引用!您可以在Firebase控制台的“存储”下找到此内容。
现在我们有了保存用户个人资料图片的方法,让我们创建一个注册新用户的方法,我们可以在IBAction上调用它:
- (void) registerNewUser
{
NSString *email = emailAddressTxtField.text;
NSString *password = passwordTxtField.text;
[[FIRAuth auth]
createUserWithEmail:email
password:password
completion:^(FIRUser *_Nullable user,
NSError *_Nullable error)
{
if (error)
{
NSLog(@"%@", error.localizedDescription);
return;
}
else
{
[self saveProfileImage];
}
}];
}
哦,太好了!现在,让我们在我们的按钮上调用该方法,以便当用户点击我们的UI上的Register按钮时,它被称为:
- (IBAction)registerUserAction:(id)sender
{
[self registerNewUser];
}
注意:请确保在ViewController.h文件中设置相应的代理:
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate>