像在 Twitter 应用程序中那样显示相机胶卷?
Display Camera Roll like in the Twitter App?
您将如何显示用户的照片,例如在 Twitter 应用程序中,照片流就在键盘下方,如下所示:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
然后写一些类似的代码:
@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
然后:
#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
此代码需要根据应用程序的要求从此处进行优化。它还需要自定义集合视图框架和布局以满足 UI 要求。
有一个 class 模仿完全相同的行为:
您可以从这里下载:Link
我使用组件 RRMessageController
https://github.com/remirobert/RRMessageController
解决了我自己的问题
您将如何显示用户的照片,例如在 Twitter 应用程序中,照片流就在键盘下方,如下所示:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
然后写一些类似的代码:
@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
然后:
#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
此代码需要根据应用程序的要求从此处进行优化。它还需要自定义集合视图框架和布局以满足 UI 要求。
有一个 class 模仿完全相同的行为:
您可以从这里下载:Link
我使用组件 RRMessageController
https://github.com/remirobert/RRMessageController