热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Firebase在用户注册期间将图像上载到存储-FirebaseUploadImagetoStorageDuringUserRegistration

ImnewtoFirebase,andIhavebeenfollowingtheirdocumentationhere.However,nothingIhavetri

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]创建具有未知类型的图像格式是错误的

2 个解决方案

#1


1  

My guess is that profilePicImageView.image doesn't actually contain a valid image object.

我的猜测是profilePicImageView.image实际上并不包含有效的图像对象。

#2


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 

推荐阅读
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
    本文旨在全面介绍Windows内存管理机制及C++内存分配实例中的内存映射文件。通过对内存映射文件的使用场合和与虚拟内存的区别进行解析,帮助读者更好地理解操作系统的内存管理机制。同时,本文还提供了相关章节的链接,方便读者深入学习Windows内存管理及C++内存分配实例的其他内容。 ... [详细]
  • 本文介绍了Windows Vista操作系统中的用户账户保护功能,该功能是为了增强系统的安全性而设计的。通过对Vista测试版的体验,可以看到系统在安全性方面的进步。该功能的引入,为用户的账户安全提供了更好的保障。 ... [详细]
  • 如何使用Python从工程图图像中提取底部的方法?
    本文介绍了使用Python从工程图图像中提取底部的方法。首先将输入图片转换为灰度图像,并进行高斯模糊和阈值处理。然后通过填充潜在的轮廓以及使用轮廓逼近和矩形核进行过滤,去除非矩形轮廓。最后通过查找轮廓并使用轮廓近似、宽高比和轮廓区域进行过滤,隔离所需的底部轮廓,并使用Numpy切片提取底部模板部分。 ... [详细]
  • ShiftLeft:将静态防护与运行时防护结合的持续性安全防护解决方案
    ShiftLeft公司是一家致力于将应用的静态防护和运行时防护与应用开发自动化工作流相结合以提升软件开发生命周期中的安全性的公司。传统的安全防护方式存在误报率高、人工成本高、耗时长等问题,而ShiftLeft提供的持续性安全防护解决方案能够解决这些问题。通过将下一代静态代码分析与应用开发自动化工作流中涉及的安全工具相结合,ShiftLeft帮助企业实现DevSecOps的安全部分,提供高效、准确的安全能力。 ... [详细]
  • SpringMVC工作流程概述
    SpringMVC工作流程概述 ... [详细]
author-avatar
davidwzw2009413
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有