Liam Robertson
Liam Robertson

Reputation: 23

UICollectionView show images stored in documents directory

Hello I can get a UITableView to display the file name of images stored in the documents folder. Is there any way to get a UICollectionView to display these images in a collection view?

Thanks Liam

Upvotes: 0

Views: 1545

Answers (2)

Pradeep Singh
Pradeep Singh

Reputation: 762

    // fetch all the files from the documents directory.
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    library = [path objectAtIndex:0];
    fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:library error:nil];

    NSArray *imagelist = [[NSArray alloc] init];

    // filter the images from all the files.
    for(int i = 0;i<fileArray.count;i++)
   {
      id object = [fileArray  objectAtIndex:i];
      if ([object rangeOfString:@".png"].location !=NSNotFound)
     {
       [imagelist addObject:object];

     }
  }

then create an instance of collection view and use following UICollectionView datasource to add the item in the collection view,

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

Upvotes: 0

Tomasz Szulc
Tomasz Szulc

Reputation: 4235

Yes you can. Here is a UICollectionView Programming Guide.

  1. Create array of file names
  2. Connect data source to UICollectionView
  3. In collectionView:cellForRowAtIndexPath: return cell with image using +imageNamed: method.

Remember to create custom cell with UIImageView inside, create outlet.

Here is Tutorial which should help you.

Upvotes: 2

Related Questions