简体   繁体   English

在UIImageView中显示存储在Firebase存储(Google云)中的图像?

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

Iv been working on a game for 6 months now and have a problem that i cant seem to fix. Iv从事游戏已有6个月了,但有一个我似乎无法解决的问题。 There are some questions similar but none exactly fitting my situation. 有一些类似的问题,但都不完全适合我的情况。

Summary 摘要

My game has 200 items. 我的游戏有200个项目。 Each Item has a Unlocked Image showing the actual Item and an Locked Image just showing it greyed out. 每个项目都有一个显示实际项目的解锁图像和一个仅显示为灰色的锁定图像。 I am going to put all 200 Unlocked Images into Firebase Storage and about 15 Locked Images (since each item has about 15 skins, the base is always the same) too. 我还将全部200张未锁定图像放入Firebase存储中,并将大约15张锁定图像(由于每个项目都有大约15个皮肤,因此基数始终相同)。

Problem 问题

The StoreViewController page is a UIViewController with a CollectionView embeded inside of it. StoreViewController页面是一个UIViewController ,其中嵌入了CollectionView When creating the CollectionViewTableCells There is the UIImageView for each cell. 创建CollectionViewTableCells ,每个单元格都有一个UIImageView Each cell iterates through a Array of Dictionaries like so (normally this Array will have 150 + dictionaries) : 每个单元都像这样遍历字典数组(通常,该数组将具有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"
    ]
]

The u-image & l-image holds the name of the appropriate image for that item. u-image & l-image保存该项目的适当图像的名称。 Those same images that are uploaded into Firebase Storage. 那些上传到Firebase存储中的相同图像。

Question

How should I use the u-image & l-image to grab the Firebase Storage and display the correct image in each UICollectionViewCell's UIImage ? 我应该如何使用u-image & l-image来获取Firebase存储并在每个UICollectionViewCell's UIImage显示正确的图像? I need to grab ~ 200 images to display each time they user goes to the StoreViewController . 每当用户访问StoreViewController时,我需要抓取200张图像来显示。

I would strongly consider replacing "u-image" and "l-image" with the public unguessable url (eg "u-image-url") that is automatically generated for your image. 我会强烈考虑用为图像自动生成的公共不可猜测网址(例如“ u-image-url”)替换“ u-image”和“ l-image”。 This Url can be downloaded without special code so you can follow steps here to add placeholders and leverage local image caching. 无需特殊代码即可下载该网址,因此您可以按照此处的步骤添加占位符并利用本地图像缓存。 You can get this public url via the firebase console and also programmatically. 您可以通过firebase控制台以及以编程方式获取此公共网址。

Via the console: 通过控制台: 在此处输入图片说明

Programmatically: 以编程方式:

// 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"]];
  }
}];

I would store this url in your database/array instead of decoding it at runtime because its simpler and you'd have a delay setting the placeholder image. 我会将这个url存储在您的数据库/数组中,而不是在运行时对其进行解码,因为它更简单,而且设置占位符图像会有延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM