Firebase存储图片网址无效

时间:2017-06-27 16:38:24

标签: ios swift firebase alamofire firebase-storage

嗨,我是来自台湾的新手开发者。 我已经开始为我的应用程序使用firebase(ios)存储,我想使用ImageSliderShow来制作图像滑块视图。从firebase获取图像URL后。我将url字符串附加到数组中然后我运行了应用程序。图像滑块视图工作正常,但新图像只显示任何内容。然后我在添加到数组之前和之后打印了url String。它给了我两个不同的字符串。这是我的代码。

override func viewDidLoad() {
    super.viewDidLoad()
    let URLString = loadURL()
    URLString.getURL(){
        (result:String) in
        self.ImageSliderView.backgroundColor = UIColor.white
        self.ImageSliderView.slideshowInterval = 5.0
        self.ImageSliderView.pageControlPosition = PageControlPosition.insideScrollView
        self.ImageSliderView.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
        self.ImageSliderView.pageControl.pageIndicatorTintColor = UIColor.black
        self.ImageSliderView.contentScaleMode = UIViewContentMode.scaleAspectFill
        self.ImageSliderView.activityIndicator = DefaultActivityIndicator()
        self.ImageSliderView.currentPageChanged = { page in
            print("current page:", page)
        }
        print("Result: \(result)")      ////print the result string before appended into array.
        self.kingfisherSource.append(KingfisherSource(urlString: String(result))!)
        print("alamoArray3: \(self.kingfisherSource[3].url)")   ////print the result string after appended into array.
        self.ImageSliderView.setImageInputs(self.kingfisherSource)
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(ResultViewController.didTap))
        self.ImageSliderView.addGestureRecognizer(recognizer)
    }
}
func didTap() {
    let fullScreenController = ImageSliderView.presentFullScreenController(from: self)
    // set the activity indicator for full screen controller (skipping the line will show no activity indicator)
    fullScreenController.slideshow.activityIndicator = DefaultActivityIndicator(style: .white, color: nil)
}

getURL函数:

class loadURL {
let storage = Storage.storage(url: "my-firebase-storage-bucket")
func getURL(completion:@escaping (_ result:String)->Void) {
    self.storage.reference().child("images/breakfast/1/1.jpg").downloadURL { url, error in
        if error != nil {
            print("Firebase Image URL error: \(String(describing: error))")
        } else {
            print("Firebase Image URL: \(String(describing: url!))")
            completion("\(String(describing: url))")
        }
    }   
}

输出:

Firebase Image URL: https://firebasestorage.googleapis.com/v0/b/my-firebase-storage-bucket/o/images%2Fbreakfast%2F1%2F1.jpg?alt=media&token=15ee8094-ac50-4e93-adc0-200793181bfc
Result: Optional(https://firebasestorage.googleapis.com/v0/b/my-firebase-storage-bucket/o/images%2Fbreakfast%2F1%2F1.jpg?alt=media&token=15ee8094-ac50-4e93-adc0-200793181bfc)
alamoArray3: Optional(https://firebasestorage.googleapis.com/v0/b/my-firebase-storage-bucket/o/images%2Fbreakfast%2F1%2F1.jpg?alt=m ... 3181bfc)

第三个输出很奇怪......是否可能是编码问题? 我对我糟糕的英语道歉。

1 个答案:

答案 0 :(得分:0)

最后我想出来了。我将结果类型作为URL,然后它才能完美运行。