ios - 在 UIImageView 中显示存储在 Firebase 存储(谷歌云)中的图像?

标签 ios swift

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

总结

我的游戏有 200 个项目。每个项目都有一个显示实际项目的解锁图像和一个仅显示为灰色的锁定图像。我将把所有 200 个解锁图像和大约 15 个锁定图像(因为每个项目有大约 15 个皮肤,基础总是相同的)也放入 Firebase 存储中。

问题

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 的 UIImage 中显示正确的图像?每次用户转到 StoreViewController 时,我都需要抓取大约 200 张图像来显示。

最佳答案

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

通过控制台: enter image description here

以编程方式:

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

我会将此网址存储在您的数据库/数组中,而不是在运行时对其进行解码,因为它更简单,并且您会延迟设置占位符图像。

关于ios - 在 UIImageView 中显示存储在 Firebase 存储(谷歌云)中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737662/

相关文章:

ios - 我可以在 UIScrollView (Swift) 中实现可伸缩图像标题吗?

objective-c - 没有 Storyboard的静态分组 UITableView

ios - 在自定义 UITextField 上关闭键盘

ios - 在scenekit ios swift中约束一个对象相对于另一个对象的位置和旋转

ios - 从collectionview中删除viewcontroller中的 subview (uiview)

ios - 无法使用 Cocoa Touch Framework 为项目创建存档

swift - UICollectionView didSelectItemAtIndexPath 影响多个单元格 - swift

ios - 如何从 Cordova 插件添加 native View

arrays - 如何打乱一组 UIViews

ios - NSCoder:无法识别的选择器发送到实例