因此,我在我的学校项目中尝试解决了一段时间的代码中出现了2个错误,但是由于我不习惯于快速编程,所以我一直无法解决它(我从YouTube教程中获取了代码)。第一个是“'StorageMetaData'的值没有成员'downloadURL'(在func uploadImg里面)。然后另一个错误是”使用未解析的标识符'imagePicker'(在@IBAction func selectedImgPicker的末尾)。 这是我的代码:
class SignUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var userImagePicker: UIImageView!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var signUpBtn: UIButton!
var userUid: String!
var emailField: String!
var passwordField: String!
var imagePicker: UIImagePickerController!
var imageSelected = false
var username: String!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
}
override func viewDidDisappear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid"){
performSegue(withIdentifier: "Message", sender: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
if let image = info[UIImagePickerControllerEditedImage] as? UIImage{
userImagePicker.image = image
imageSelected = true
} else {
print("image wasnt selected")
}
imagePicker.dismiss(animated: true, completion: nil)
}
func setUser(img: String){
let userData = ["username": username!, "userImg": img]
KeychainWrapper.standard.set(userUid, forKey: "uid")
let location = Database.database().reference().child("users").child(userUid)
location.setValue(userData)
dismiss(animated: true, completion: nil)
}
func uploadImg(){
if usernameField.text == nil {
signUpBtn.isEnabled = false
} else {
username = usernameField.text
signUpBtn.isEnabled = true
}
guard let img = userImagePicker.image, imageSelected == true else{
print("image needs to be selected")
return
}
if let imgData = UIImageJPEGRepresentation(img, 0.2){
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
Storage.storage().reference().putData(imgData, metadata: metadata){
(metadata, error) in
if error != nil{
print("did not upload image")
}else{
print("uploaded")
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL{
self.setUser(img: url)
}
}
}
}
}
@IBAction func createAccount(_ sender: AnyObject){
Auth.auth().createUser(withEmail: emailField, password: passwordField, completion: {
(user, error) in
if error != nil {
print("Cant create user")
} else {
if let user = user {
self.userUid = user.user.uid
}
}
self.uploadImg()
})
}
@IBAction func selectedImgPicker (_ sender: AnyObject){
present(imagePicker, animated: true, completion: nil)
}
@IBAction func cancel (_ sender: AnyObject){
dismiss(animated: true, completion: nil)
}
}