2016-09-20 56 views
0

我想從我的Firebase/Storage下載關於上傳時間的圖片。 這意味着,最後上傳的圖片將成爲我新聞的第一張圖片(有點像instagram)。從「Firebase/Storage」下載關於「上次修改」的圖片

我怎麼寫這樣的代碼? 我真的不知道從哪裏開始,製作ImageArray,還是必須定義每個UIImageView? 我無法找到Firebase提供的關於此主題的幫助。

幫助表示讚賞。 謝謝!

+0

http://stackoverflow.com/a/39437711/6297658 – Dravidian

+0

謝謝,但正如我所說我正在使用Firebase-Storage。 我現在是否整合Firebase /數據庫來顯示圖像? – Leo

+0

您必須將這些圖像鏈接保存在某個地方?保存與帖子的鏈接..或找出解決方法,,, :)快樂編碼 – Dravidian

回答

2

我們強烈建議您一起使用Firebase存儲和Firebase實時數據庫來完成此操作。下面是類似的東西一個完整的例子:

共享:

// Firebase services 
var database: FIRDatabase! 
var storage: FIRStorage! 
... 
// Initialize Database, Auth, Storage 
database = FIRDatabase.database() 
storage = FIRStorage.storage() 
... 
// Initialize an array for your pictures 
var picArray: [UIImage]() 

上傳:

let fileData = NSData() // get data... 
let storageRef = storage.reference().child("myFiles/myFile") 
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in 
    // When the image has successfully uploaded, we get it's download URL 
    let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString 
    // Write the download URL to the Realtime Database 
    let dbRef = database.reference().child("myFiles/myFile") 
    dbRef.setValue(downloadURL) 
} 

下載:

let dbRef = database.reference().child("myFiles") 
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in 
    // Get download URL from snapshot 
    let downloadURL = snapshot.value() as! String 
    // Create a storage reference from the URL 
    let storageRef = storage.referenceFromURL(downloadURL) 
    // Download the data, assuming a max size of 1MB (you can change this as necessary) 
    storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in 
    // Create a UIImage, add it to the array 
    let pic = UIImage(data: data) 
    picArray.append(pic) 
    }) 
}) 

在這一點上,你可以簡單地使用圖像picArray將它們顯示在tableView中。您甚至可以使用Firebase數據庫查詢按文件時間戳或其他信息(當您將URL寫入數據庫時​​要寫入該信息)進行排序查詢。

欲瞭解更多信息,請參閱Zero to App: Develop with Firebase和它的associated source code,這是一個實例。