[英]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. 确实,您应该分解问题,仅对每个问题提出一个问题,但是我会为您提供一些指导。
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.