【问题标题】:How to download all Images in Folder in Firebase Storge ( Objective-C)如何下载 Firebase Storge 文件夹中的所有图像(Objective-C)
【发布时间】:2016-07-06 03:17:04
【问题描述】:

我在 Storage Firebase 中有一个文件夹图像,我想下载所有图像并在我的收藏视图中显示它们。 但是当我想在文件夹中获取图像时出现问题。 我该怎么做。

感谢您的帮助!

self.storageRef = [[FIRStorage storage] reference];
self.imagesArray = [[NSMutableArray alloc]init];


[[self.storageRef child:@"IMAGES"] dataWithMaxSize:1 * 1024 * 1024 completion:^(NSData *data, NSError *error) {
    UIImage *image = [UIImage imageWithData:data];
    [self.imagesArray addObject:image];

}];
self.imageView.image = self.imagesArray.firstObject;

//我的错误:原因:'*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

【问题讨论】:

标签: objective-c firebase-storage


【解决方案1】:

查看代码后,问题在于对dataWithMaxSize:completion: 的调用是异步的(这意味着执行需要时间,因此在调用完成块之前会转到下一行)。这是被称为的东西:

// This runs first 
[[self.storageRef child:@"IMAGES"] dataWithMaxSize:1 * 1024 * 1024 completion:^(NSData *data, NSError *error) {
    // This runs third (well, not at all since the program crashes)
    UIImage *image = [UIImage imageWithData:data];
    [self.imagesArray addObject:image];

}];
// This runs second, and thus [self.imagesArray count] == 0
self.imageView.image = self.imagesArray.firstObject;

相反,您需要执行以下操作:

// This runs first 
[[self.storageRef child:@"IMAGES"] dataWithMaxSize:1 * 1024 * 1024 completion:^(NSData *data, NSError *error) {
    // This runs second
    UIImage *image = [UIImage imageWithData:data];
    [self.imagesArray addObject:image];
    // This runs third, and now [self.imagesArray count] == 1
    // so this doesn't crash the program
    self.imageView.image = self.imagesArray.firstObject;
}];

虽然你会注意到,如果你这样做,你不再需要数组。

【讨论】:

  • 谢谢你的帮助兄弟!但我还是做不到。 self.storageRef = [[FIRStorage 存储] 参考]; self.imagesArray = [[NSMutableArray alloc]init]; [[self.storageRef child:@"IMAGES"] dataWithMaxSize:1 * 1024 * 1024 完成:^(NSData data, NSError *error) { UIImage *image = [UIImage imageWithData:data]; [self.imagesArray addObject:image]; }]; self.imageView.image = self.imagesArray.firstObject;我的错误:原因:'** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
  • 检查是否有错误——您完全有可能遇到错误并因此创建一个nil 对象,从而将nil 添加到字典中(您不能这样做) .
  • 有人跟我说话,从 Json 数据库中保存图像的 imageUrl。加载所有 Json 数据库,不关心 Storage 中的 Storage 或 IndexPath images 文件夹。我已经尝试过了,但是我的 tableview 显示图像看起来不太好。它加载缓慢并更改索引(例如:我有 5 张图像,当我再次滚动 tableview 时,它们只会重播 1 张图像、5 个单元格但只是相同的图像......我错了什么?非常感谢兄弟,Mike McDonald .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2021-12-28
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
相关资源
最近更新 更多