Search code examples
objective-cxcodeimageios7image-gallery

Display local images in gallery objective-c


I would like to display images that were saved locally from the app to the iPhones Documents directory in a gallery layout, similar to Photos.app with the thumbnails (4 columns, as many rows necessary). I have tried some open sourced code, including CHGridView, but most of them are several years old and not working with iOS 7. I would prefer to not use open sourced code if possible anyway.

I am able to store my files in an NSMutableArray using the following method:

- (NSMutableArray *)arrayOfImages
{
    int count;
    NSMutableArray *array = [NSMutableArray array];
    for (count = 0; count < 500; count++) //assuming no more than 500 items saved.
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *fileName = [NSString stringWithFormat:@"img%d", count]; //images are called img0.png, img1.png, etc
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
        UIImage *checkImage = [[UIImage alloc] initWithContentsOfFile:filePath];

        if (checkImage != nil) //if there is an image there, add it to array
        {
            [array addObject:checkImage];
        }
    }
    return array;
}

Solution

  • UICollectionView was added in iOS6 and is designed to do exactly what you are describing. The code needed is similar to the code for a UITableView, but instead of managing a 1D collection like a table view, the collection view manages a 2D collection of objects.