0

I have a Folder Images in Storage Firebase, I want to download all images and show them in my collectionview. But I have a problem when I want to get Index an image in Folder. How can I do that .

Thank you for your help !

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;

//My error : reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

Picture of Storage Firebase

Kai Luu
  • 19
  • 1
  • 6
  • 1
    Possible duplicate of [How to get an array with all pictures?](http://stackoverflow.com/questions/37335102/how-to-get-an-array-with-all-pictures) – Mike McDonald Jul 06 '16 at 03:30

1 Answers1

1

After taking a look at the code, the issue is that the call to dataWithMaxSize:completion: is async (meaning that it takes time to execute and thus goes to the next line before calling the completion block). This is the oder things are being called:

// 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;

Instead, you need to do something like:

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

Though you'll note that you no longer need the array if you're doing this.

Mike McDonald
  • 15,609
  • 2
  • 46
  • 49
  • Thank you for your help Bro ! But I still can't do it. 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; My error : reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' – Kai Luu Jul 06 '16 at 13:00
  • Check if there's an error--it's entirely possible that you're getting an error and therefore making a `nil` object, thus adding `nil` to a dictionary (which you can't do). – Mike McDonald Jul 06 '16 at 22:59
  • someone talk with me save imageUrl of image from Json Database. Load all Json database and don't care about Storage or IndexPath images Folder in Storage. I already try and it worked but my tableview show images look not good. It load slowly and change index ( Ex: I have 5 images and when I scroll the tableview again them will replay just 1 image, 5 cell but just the same image...what was I wrong ? Thank you so much bro, Mike McDonald. – Kai Luu Jul 08 '16 at 06:49