【问题标题】:UploadPhoto with Swift and Firebase使用 Swift 和 Firebase 上传照片
【发布时间】:2020-04-12 12:50:04
【问题描述】:

我在上传照片作为用户的个人资料照片时遇到了一些困难。这是我的代码的一些设置。 这就是我设置用户类的方式。

import UIKit
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
class Users: NSObject {
    var username : String = ""
    var email : String = ""
    var uid : String = ""
    var profilePicLink : String
    init(username : String, email: String, uid : String, profilePicLink: String ) {
        self.username = username
        self.email    = email
        self.uid      = uid
        self.profilePicLink = profilePicLink
    }
    func getProfileImage() -> UIImage {
        if let url = NSURL(string: profilePicLink){
            if let data = NSData(contentsOf: url as URL) {
                return UIImage(data: data as Data)!
            }
        }
        return UIImage()
    }
    func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
            } else {
                // failed
                completion(nil)
            }
        }
    }

  }

在 SettingView 类中,我调用了 func uploadProfileImage,但是,我得到了错误“Editor placeholder in source file”。请建议我应该在设置视图类中的函数 uploadPhoto 中传递什么值

import UIKit

class SettingView: UIViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {

    @IBOutlet var imageView: UIImageView!

    @IBOutlet var displayName: UILabel!
    var selectedUser:Users?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(displayName.text == selectedUser?.username)
    }
    @IBAction func getPhotoButton(_ sender: Any) {
        let image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(image, animated: true, completion: nil)
    }
    @IBAction func updatePhoto(_ sender: Any) {
        uploadPhoto()
    }
    func uploadPhoto(){
        selectedUser?.uploadProfileImage(imageView.image!, completion: <#(URL?) -> ()#>) **Error : Editor placeholder in source file**

    }
    private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let pickerInfo: NSDictionary = info as NSDictionary
        let img : UIImage = pickerInfo.object(forKey: UIImagePickerController.self) as! UIImage
        imageView.image = img
        self.dismiss(animated: true, completion: nil)
    }

【问题讨论】:

    标签: swift firebase


    【解决方案1】:

    XCode 抱怨是因为您在 uploadPhoto 方法中有编辑器占位符 completion: &lt;#(URL?) -&gt; ()#&gt;)。将uploadPhoto 方法替换为:

    func uploadPhoto(){
        selectedUser?.uploadProfileImage(imageView.image!) { url in
    
            print(url)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 1970-01-01
      • 2021-12-02
      • 2017-05-09
      • 1970-01-01
      • 2017-03-30
      • 2017-03-06
      • 2019-04-18
      • 2017-11-20
      相关资源
      最近更新 更多