繁体   English   中英

在Twitter App中显示相机胶卷?

[英]Display Camera Roll like in the Twitter App?

如何在Twitter应用程序中显示用户照片,其中照片流在键盘下方,如下所示:

在此输入图像描述

在此输入图像描述

在此输入图像描述

添加AssetsLibrary框架

然后写一些类似于的代码:

@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.

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