最后一行代码:self.setUpUser(img: url)
有问题。请帮我修复它。
我认为那行代码是Swift的旧版本。但是,我还尝试了它的新版本self.SetUpUser(Image: url!.absoluteString)
,它也会弹出很多错误。
创建的错误是:
无法将“无效”类型的值转换为预期的参数类型“字符串”
如下所示:
我真的需要一些帮助。
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import SwiftKeychainWrapper
class UserVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var userImagePicker: UIImageView!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var completeSignInBtn: 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 = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
}
func keychain(){
KeychainWrapper.standard.set(userUid, forKey: "uid")
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
userImagePicker.image = image
imageSelected = true
} else {
print("image wasn't selected")
}
imagePicker.dismiss(animated: true, completion: nil)
}
func setUpUser(img: String){
let userData = [
"username": username!,
"userImg": img
]
keychain()
let setLocation = Database.database().reference().child("users").child(userUid)
setLocation.setValue(userData)
}
func uploadImg() {
if usernameField.text == nil {
print("must have username")
completeSignInBtn.isEnabled = false
} else {
username = usernameField.text
completeSignInBtn.isEnabled = true
}
guard let img = userImagePicker.image, imageSelected == true else {
print("image must be selected")
return
}
if let imgData = img.jpegData(compressionQuality: 0.2) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "img/jpeg"
Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { (metadata, error) in
if error != nil {
print("did not upload img")
} else {
print("uploaded")
let downloadURl = metadata?.storageReference?.downloadURL(completion: { (url, error) in if error != nil {
print(error as Any)
}})
if let url = downloadURl {
self.setUpUser(img: url)
}
}
}
}
}
@IBAction func completeAccount(_ sender: Any){
Auth.auth().createUser(withEmail: emailField, password: passwordField, completion: { (user,error) in
if error != nil {
print("can't create user \(error)")
} else {
if let user = user {
self.userUid = user.user.uid
}
}
self.uploadImg()
})
dismiss(animated: true, completion: nil)
}
@IBAction func seletedImagePicker(_ sender: Any){
present(imagePicker, animated: true, completion: nil)
}
@IBAction func cancel(_ sender: AnyObject){
dismiss(animated: true, completion: nil)
}
}
答案 0 :(得分:0)
请检查以下代码
var imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.5)!)
self.uploadProfileImageToFirebase(data: imgData)
func uploadProfileImageToFirebase(data:NSData){
guard let userID = Auth.auth().currentUser?.uid else {
return
}
let storageRef = Storage.storage().reference().child("Pics").child("\(userID).jpg")
if data != nil {
storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}
// Fetch the download URL
storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
return
}
} else {
// Get the download URL for 'images/stars.jpg'
let urlStr:String = (url?.absoluteString) ?? ""
/////
self.setUpUser(img: urlStr)
}
}
})
}
}