I'm new to Firebase, and I have been following their documentation here. However, nothing I have tried seems to work.
我是Firebase的新手,我一直关注他们的文档。但是,我尝试过的任何东西似乎都没有用。
What I am trying to do:
我想做什么:
1) Register a user -Works
1)注册用户-Works
2) Have the user choose a profile picture during the registration process -Doesn't work.
2)让用户在注册过程中选择个人资料图片 - 不起作用。
The code:
代码:
- (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");
}
}];
}
}
Additional Information:
附加信息:
Photos are coming only from the camera roll of the user's device Debug area prints:
照片仅来自用户设备的相机胶卷调试区域打印:
[Generic] Creating an image format with an unknown type is an error
[Generic]创建具有未知类型的图像格式是错误的
1
My guess is that profilePicImageView.image doesn't actually contain a valid image object.
我的猜测是profilePicImageView.image实际上并不包含有效的图像对象。
1
HOW TO REGISTER A USER
如何注册用户
Let's assume that you have 3 textfields to register a user. You have a usernameTextfield, an emailTextField, and a passwordTextfield. We also want to have a profilePicture associated with this user. So we first:
假设您有3个文本字段来注册用户。您有usernameTextfield,emailTextField和passwordTextfield。我们还希望将profilePicture与此用户相关联。所以我们先:
Established a method to save our values for our User to the Firebase Database:
建立了一种方法,将我们的用户值保存到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}];
}
The profileImageURL above is an NSString that you can create above your implementation of your view controller, but we also should create a UIImage that we can access throughout the ViewController:
上面的profileImageURL是一个NSString,您可以在视图控制器的实现之上创建,但我们也应该创建一个我们可以在整个ViewController中访问的UIImage:
#import "ViewController.h"
@import Photos;
@interface ViewController ()
@end
NSString *profileImageURL;
UIImage *profileImage;
@implementation ViewController
I'm guessing that you know how to launch the camera roll off from a UIButton but in case you do not, let's call these methods so that when our user taps the button to choose their image that it get's set to our profilePicture.imageView:
我猜你知道如何从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 *)info
{
picker.delegate = self;
profileImage = info[UIImagePickerControllerEditedImage];
profilePicImageView.image = profileImage;
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Now we're cooking with fire! Now that we have the image the user wants to upload for their profilePicture, let's create a method to save it as an NSURL to our Firebase Database:
现在我们正在用火烹饪!现在我们已经拥有了用户想要为其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");
}
}];
}
}
IMPORTANT: Make sure you insert your gs:// reference for the value of storageRef! You can find this on your Firebase Console under Storage.
重要信息:确保插入gs:// reference以获取storageRef的值!您可以在存储下的Firebase控制台上找到此信息。
Now that we have the method to save the user's profile picture, let's create a method to Register the new user that we can call it on our IBAction:
现在我们有了保存用户个人资料图片的方法,让我们创建一个注册新用户的方法,我们可以在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];
}
}];
}
Oh thats nice! Now, let's call that method on our button, so that when the user taps the Register button on our UI, that it get's called:
哦,那很好!现在,让我们在我们的按钮上调用该方法,这样当用户点击我们的UI上的Register按钮时,就会调用它:
- (IBAction)registerUserAction:(id)sender
{
[self registerNewUser];
}
Note: Make sure that in your ViewController.h file that you are setting the appropriate delegates:
注意:确保在ViewController.h文件中设置了相应的委托:
@interface ViewController : UIViewController