从Swift上的Firebase通过URL下载时,图像横向加载

时间:2017-02-28 06:36:38

标签: ios firebase swift3 firebase-storage

我目前正在从Firebase上传和下载图片。我将URL作为字符串存储在变量profilePicture中,然后使用URL会话下载图像。但是,每次照片都在侧面加载。但是,当我最初从相机胶卷中选择照片时,会以正确的方式显示。关于可能发生什么的任何想法?

上传图片: 当我上传图片时,一旦完全保存,我将URL设置为变量profilePicture。

    var storageRef : FIRStorageReference = FIRStorageReference()
    if let uid = currentUser?.uid {
        storageRef = FIRStorage.storage().reference().child("\(uid).png")
    }

    if let uploadData = UIImagePNGRepresentation(profilePictureImageView.image!) {

        let uploadTask = storageRef.put(uploadData, metadata: nil) { snapshot, error in
            if let error = error {
                print(error)
            }
        }

        // Shows progress bar until saved
        uploadTask.observe(.progress) { snapshot in
            self.progressBar.observedProgress = snapshot.progress
            self.progressBar.isHidden = false;
        }

        uploadTask.observe(.success) { snapshot in
            if let profileImageURL = snapshot.metadata?.downloadURL()?.absoluteString {
                self.user?.profilePicture = profileImageURL
                self.progressBar.isHidden = true;
                self.savedLabel.isHidden = false

            }
        }
    }

正在下载图片:

 if let profileImageURL = user?.profilePicture {
        if let url = URL(string: profileImageURL) {
            URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in

                if error != nil {
                    print(error!)
                    return
                }

                DispatchQueue.main.async {
                    self.profilePictureImageView.image = UIImage(data: data!)
                    self.activityIndicator.stopAnimating()
                }
            }).resume()
        }
    }

1 个答案:

答案 0 :(得分:0)

我觉得它与图像方向问题有关,而不是Firebase中的任何问题。像iOS Image Orientation has Strange Behavior

这样的东西