简体   繁体   中英

Display Camera Roll like in the Twitter App?

How would you display a users photos like in the twitter application where the photostream is right under the keyboard like this:

在此输入图像描述

在此输入图像描述

在此输入图像描述

添加AssetsLibrary框架

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.

结果截图

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

There's a class that mimics that exact same behaviour:

在此输入图像描述

You can download it from here: Link

我通过使用组件RRMessageController https://github.com/remirobert/RRMessageController解决了我自己的问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM