简体   繁体   English

IOS-限制用户最多选择8张图像,并使用For循环将多张图像上传到Firebase

[英]IOS - Limit user to choose at most 8 images and upload multiple images to Firebase using a For-loop

I am new to IOS. 我是IOS的新手。 I am building an image sharing app with Xcode and Swift 我正在使用Xcode和Swift构建图像共享应用程序

I am stuck in images uploading part. 我被卡在图片上传部分。

How can I limit users to choose 8 images at most and then upload the chosen pictures to Firebase using a For-loop? 如何限制用户最多选择8张图片,然后使用For循环将所选图片上传到Firebase?

I would like to use For-loop since I want to set the first picture as an icon picture. 我想使用For-loop,因为我想将第一张图片设置为图标图片。

Below is what I am able to do now 以下是我现在能做的

I created a SignUp page and allow a user to use ImagePicker to select 1 image and upload it to Firebase. 我创建了一个SignUp页面,并允许用户使用ImagePicker选择1张图像并将其上传到Firebase。 Below is my code. 下面是我的代码。

import UIKit
import Firebase

class SignUpViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
{

    @IBOutlet weak var iconfield: UIImageView!

    @IBOutlet weak var usernamefield: UITextField!

    @IBOutlet weak var emailfield: UITextField!

    @IBOutlet weak var passwordfield: UITextField!

    @IBOutlet weak var confirmpasswordfield: UITextField!

    @IBOutlet weak var signupbutton: UIButton!

    let picker = UIImagePickerController()
    var userStorage: StorageReference!
    var ref: DatabaseReference!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        picker.delegate = self
        let storage = Storage.storage().reference(forURL: "gs://whatisit-8484a.appspot.com/")
        ref = Database.database().reference()
        userStorage = storage.child("users")
        // Do any additional setup after loading the view.
        iconfield.layer.cornerRadius=iconfield.frame.size.width/2
        iconfield.clipsToBounds = true
    }

    @IBAction func selectionpressed(_ sender: Any)
    {
        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        present(picker,animated:true,completion:nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage
        {
            self.iconfield.image = image
            signupbutton.isHidden = false
        }
        self.dismiss(animated:true,completion:nil)
    }

    @IBAction func confirmsignup(_ sender: Any)
    {
        guard usernamefield.text != "",emailfield.text != "",passwordfield.text != "", confirmpasswordfield.text != "" else {return}

        if passwordfield.text == confirmpasswordfield.text
        {
            Auth.auth().createUser(withEmail: emailfield.text!, password: passwordfield.text!, completion: { (user, error) in
                if let error = error
                {
                    print(error.localizedDescription)
                }

                if let user = user
                {
                    let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
                    changeRequest.displayName = self.usernamefield.text!
                    changeRequest.commitChanges(completion: nil)

                    let imageRef = self.userStorage.child("\(user.uid).jpg")
                    let data = UIImageJPEGRepresentation(self.iconfield.image!, 0.5)

                    let uploadTask = imageRef.putData(data!, metadata: nil, completion: { (metadata, err) in
                        if err != nil{
                            print(err!.localizedDescription)
                        }
                        imageRef.downloadURL(completion: { (url, er) in
                            if er != nil{
                                print(er!.localizedDescription)
                            }

                            if let url = url
                            {
                                let userInfo: [String: Any] = ["uid": user.uid,"E-mail":self.emailfield.text,"username": self.usernamefield.text,"urlToImage": url.absoluteString]
                                self.ref.child("users").child(user.uid).setValue(userInfo)
                                let vc = UIStoryboard(name: "Main",bundle:nil).instantiateViewController(withIdentifier: "homeview")
                                self.present(vc,animated:true,completion:nil)
                            }
                        })
                    })
                    uploadTask.resume()
                }
            })
        }

Your question is quite broad and actually covers a few different questions. 您的问题范围很广,实际上涵盖了几个不同的问题。 Really, you should break the problem down and only post a question for each, but I will give you some guidance. 确实,您应该分解问题,仅对每个问题提出一个问题,但是我会为您提供一些指导。

  • How to select multiple images using a UIImagePickerController - Basically you can't, you either need to use a library or do your own implementation using the PhotoLibrary 如何使用UIImagePickerController选择多个图像 -基本上,您不能使用库,也可以使用PhotoLibrary自己实现
  • How to upload an image to Firebase should be fairly straight forward. 如何将图像上传到Firebase应该很简单。
  • Multiple uploads can be a bit more complex. 多次上传可能会更加复杂。 You could use a NSOperationQueue to manage a queue of items to process and specify how many to handle at once, you could use a SerialQueue to process items one at a time or a DispatchQueue to fire all of the requests at once and it will let you know when it finishes. 您可以使用NSOperationQueue管理要处理的项目队列并指定一次处理的数量,可以使用SerialQueue一次处理一个项目,也可以使用DispatchQueue一次触发所有请求,它将使您知道什么时候结束。 Once you reach this point, firstly give it a try, look into the mentioned methods. 一旦达到这一点,请首先尝试一下,看看上述方法。 If you get stuck, raise another question and explain your approach, how you want it to work and what is going wrong. 如果您陷入困境,请提出另一个问题并解释您的方法,您希望它如何工作以及出了什么问题。

This other answer may help with the multiple uploads, if not have a look for Grand Central Dispatch online or check here 如果没有在线查找Grand Central Dispatch或在此处查看, 此其他答案可能有助于多次上传

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM