图像选择器Swift

时间:2018-07-19 19:56:24

标签: swift firebase firebase-storage

我想让用户随后选择个人资料图片。 我正在使用Firebase进行存储。 但是我仍然得到一个错误。 我尝试了几次,但无法修复...。 有谁知道更好的代码或如何修复我的代码? 感谢您的帮助

ERROR是 使用未解决的标识符“ profilePic” 使用未解决的标识符“存储”

@IBOutlet weak var ImageView: UIImageView!
    let imagePicker = UIImagePickerController()
   
    @IBAction func loadImageButtonTapped(_ sender: Any) {
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary
        
        present(imagePicker, animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)    
        
       guard let uid = Auth.auth().currentUser?.uid else {return}
       guard let imageData = UIImageJPEGRepresentation(profilePic, 0.5) else {return}
        let profileImgReference = Storage.storage().reference().child("profile_image_urls").child("\(uid).png")
        let uploadTask = profileImgReference.putData(imageData, metadata: nil) { (metadata, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                let downloadURL = metadata?.downloadURL()?.absoluteString ?? ""
                // Here you get the download url of the profile picture.
            }
        }
        uploadTask.observe(.progress, handler: { (snapshot) in
            print(snapshot.progress?.fractionCompleted ?? "")
            // Here you can get the progress of the upload process.
        })
        }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            ImageView.contentMode = .scaleAspectFit
            ImageView.image = pickedImage
        }
        
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}

0 个答案:

没有答案