[英]Display Camera Roll like in the Twitter App?
如何在Twitter應用程序中顯示用戶照片,其中照片流在鍵盤下方,如下所示:
然后寫一些類似於的代碼:
@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要求。
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
我通過使用組件RRMessageController
https://github.com/remirobert/RRMessageController解決了我自己的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.