簡體   English   中英

在UIImageView中顯示存儲在Firebase存儲(Google雲)中的圖像?

[英]Show Image Stored in Firebase Storage (Google Cloud) In UIImageView?

Iv從事游戲已有6個月了,但有一個我似乎無法解決的問題。 有一些類似的問題,但都不完全適合我的情況。

摘要

我的游戲有200個項目。 每個項目都有一個顯示實際項目的解鎖圖像和一個僅顯示為灰色的鎖定圖像。 我還將全部200張未鎖定圖像放入Firebase存儲中,並將大約15張鎖定圖像(由於每個項目都有大約15個皮膚,因此基數始終相同)。

問題

StoreViewController頁面是一個UIViewController ,其中嵌入了CollectionView 創建CollectionViewTableCells ,每個單元格都有一個UIImageView 每個單元都像這樣遍歷字典數組(通常,該數組將具有150多個字典):

let items : [[String:String]] = [

    [
        "type" : "star",
        "model" : "blackOne",
        "available" : "no",
        "rarity" : "normal",
        "totalOwned" : "1",
        "price" : "9,999",
        "u-image" : "blackone-u",
        "l-image" : "star-l"
    ],
    [
        "type" : "star",
        "model" : "blackTwo",
        "available" : "no",
        "rarity" : "normal",
        "totalOwned" : "1",
        "price" : "10,500",
        "u-image" : "blacktwo-u",
        "l-image" : "star-l"
    ],
    [
        "type" : "square",
        "model" : "black-New",
        "available" : "no",
        "rarity" : "normal",
        "totalOwned" : "1",
        "price" : "12,950",
        "u-image" : "blacknew-u",
        "l-image" : "square-l"
    ]
]

u-image & l-image保存該項目的適當圖像的名稱。 那些上傳到Firebase存儲中的相同圖像。

我應該如何使用u-image & l-image來獲取Firebase存儲並在每個UICollectionViewCell's UIImage顯示正確的圖像? 每當用戶訪問StoreViewController時,我需要抓取200張圖像來顯示。

我會強烈考慮用為圖像自動生成的公共不可猜測網址(例如“ u-image-url”)替換“ u-image”和“ l-image”。 無需特殊代碼即可下載該網址,因此您可以按照此處的步驟添加占位符並利用本地圖像緩存。 您可以通過firebase控制台以及以編程方式獲取此公共網址。

通過控制台: 在此處輸入圖片說明

以編程方式:

// Get a reference to the storage service, using the default Firebase App
FIRStorage *storage = [FIRStorage storage];
// Create a storage reference from our storage service
FIRStorageReference *storageRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>"];
FIRStorageReference *imagesRef = [storageRef child:<uimageString_from_array + '.png'>];
[imagesRef downloadURLWithCompletion:^(NSURL URL, NSError error){
  if (error != nil) {
    // Handle any errors
  } else {
    [cell.imageView setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
  }
}];

我會將這個url存儲在您的數據庫/數組中,而不是在運行時對其進行解碼,因為它更簡單,而且設置占位符圖像會有延遲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM