Search code examples
iosobjective-ciphoneheaderphotos

iOS how to place header in collection view of date between photos as in iPhone photos app


i want to classify photo according to date in the collection view and add header between it same like in Photos of iPhone main this is classifying the photos according to the date then header i can place there... help will be appreciated thank you...


Solution

  • NSArray<PHAsset *> *imageAssets = @[];
    NSMutableDictionary<NSString *, NSMutableArray *> *groupedAssets = [NSMutableDictionary dictionary];
    for (PHAsset *imageAsset in imageAssets) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM dd, yyyy"];
        NSString *key = [dateFormatter stringFromDate:imageAsset.creationDate];
    
        // Add asset to group
        NSMutableArray *group = [groupedAssets objectForKey:key];
        if (!group) {
            group = [NSMutableArray array];
            [groupedAssets setObject:group forKey:key];
        }
        [group addObject:imageAsset];
    }
    

    This is an idea about how can you approach your problem. Above code assumes that you have an array of PHAssets. The groupedAssets object you receive at the end of the loop is collection of assets grouped by day.