【问题标题】:Show Image Stored in Firebase Storage (Google Cloud) In UIImageView?在 UIImageView 中显示存储在 Firebase 存储(谷歌云)中的图像?
【发布时间】:2016-06-11 02:29:02
【问题描述】:

我已经开发了 6 个月的游戏,但遇到了一个我似乎无法解决的问题。有一些类似的问题,但没有一个完全适合我的情况。

总结

我的游戏有 200 个项目。每个项目都有一个显示实际项目的未锁定图像和一个仅显示其灰色的锁定图像。我将把所有 200 张未锁定图像和大约 15 张锁定图像放入 Firebase 存储中(因为每个项目都有大约 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 张图片来显示。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    我强烈考虑将“u-image”和“l-image”替换为为您的图片自动生成的公共不可猜测的 url(例如“u-image-url”)。无需特殊代码即可下载此 URL,因此您可以按照步骤 here 添加占位符并利用本地图像缓存。 您可以通过 firebase 控制台获取此公共 url,也可以通过编程方式获取。

    通过控制台:

    以编程方式:

    // 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 存储在您的数据库/数组中,而不是在运行时对其进行解码,因为它更简单,并且您在设置占位符图像时会有延迟。

    【讨论】:

    • 本杰明真棒!我有另一个问题。根据项目的“类型”,大约有 10 种不同的占位符图像。我将如何为每种类型的枪显示适当的占位符?这就是为什么我有“u-image”(解锁图像)和“l-image”(锁定图像占位符)的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2017-05-06
    相关资源
    最近更新 更多