在Twitter App中显示相机胶卷?

时间:2015-01-06 03:59:41

标签: ios iphone twitter camera photo

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

enter image description here

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:1)

Add the AssetsLibrary framework

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

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

Screenshot of Result

答案 1 :(得分:0)

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

答案 2 :(得分:0)

class模仿完全相同的行为:

enter image description here

您可以从此处下载:Link

答案 3 :(得分:0)

我使用组件RRMessageController https://github.com/remirobert/RRMessageController

解决了我自己的问题