【问题标题】:Display Camera Roll like in the Twitter App?像在 Twitter 应用程序中一样显示相机胶卷?
【发布时间】:2015-03-03 17:17:32
【问题描述】:

您将如何在 twitter 应用程序中显示用户照片,其中照片流位于键盘下方,如下所示:

【问题讨论】:

  • 我不知道 Twitter 是如何实际实现的 (duh),但是您可以使用容器来实现这一点。只需将UIImagePicker 创建到内存中,而不是使用presentViewController...,只需将其添加为子视图控制器即可。我还没有测试过这种方法,所以我会在有人这样做之前发表评论。

标签: ios iphone twitter camera photo


【解决方案1】:

然后写一些类似的代码:

@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 要求。

【讨论】:

  • 更新 UI 以显示 self.thumbnails 是什么意思,能给我举个一般的例子吗?
  • 正确地做这件事涉及很多代码,但可以肯定。
【解决方案2】:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

【讨论】:

  • 只是在点击按钮时显示它,我希望它默认显示在视图控制器中。
【解决方案3】:

有一个 class 模仿了完全相同的行为:

您可以从这里下载:Link

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      相关资源
      最近更新 更多