How would you display a users photos like in the twitter application where the photostream is right under the keyboard like this:
Then write some code similar to:
@interface ImageCell : UICollectionViewCell
@property (strong) UIImageView *imageView;
@end
@implementation ImageCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.imageView = [UIImageView new];
[self addSubview:self.imageView];
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = self.bounds;
}
@end
Then:
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()
@property (strong) NSMutableArray *thumbnails;
@property (strong) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectInset(self.view.bounds, 20, 20)
collectionViewLayout:[UICollectionViewFlowLayout new]];
self.collectionView.backgroundColor = [UIColor clearColor];
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"Cell"];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.view addSubview:self.collectionView];
ALAssetsLibrary *library = [ALAssetsLibrary new];
self.thumbnails = [NSMutableArray new];
__weak ViewController *weakSelf = self;
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
ViewController *strongSelf = weakSelf;
if(!result.thumbnail)
return;
[strongSelf.thumbnails addObject:[UIImage imageWithCGImage:result.thumbnail]];
[strongSelf updateUI];
}];
} failureBlock:^(NSError *error){}];
}
- (void)updateUI
{
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.thumbnails.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = self.thumbnails[indexPath.row];
return image.size;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.imageView.image = self.thumbnails[indexPath.row];
return cell;
}
@end
This code needs to be optimized from here depending on the requirements of the app. It also needs to customize the collection view frame and layout to match UI requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With