Ios 在Twitter应用程序中显示相机卷?
在twitter应用程序中,photostream就在键盘下面,您会如何显示用户的照片,如下所示:Ios 在Twitter应用程序中显示相机卷?,ios,iphone,twitter,camera,photo,Ios,Iphone,Twitter,Camera,Photo,在twitter应用程序中,photostream就在键盘下面,您会如何显示用户的照片,如下所示: UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; imagePickerController.sourceT
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
然后:
#导入“ViewController.h”
#进口
@界面视图控制器()
@属性(强)NSMutableArray*缩略图;
@属性(强)UICollectionView*collectionView;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectInset(self.view.bounds,20,20)
collectionViewLayout:[UICollectionViewFlowLayout新建]];
self.collectionView.backgroundColor=[UIColor clearColor];
[self.collectionView注册表类:[ImageCell类]forCellWithReuseIdentifier:@“Cell”];
self.collectionView.dataSource=self;
self.collectionView.delegate=self;
[self.view addSubview:self.collectionView];
ALAssetsLibrary*库=[ALAssetsLibrary新建];
self.thumbnails=[NSMutableArray new];
__弱视图控制器*weakSelf=self;
[库枚举组SwithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup*组,布尔*停止){
[组EnumerateAssetSingBlock:^(ALAsset*结果,NSU整数索引,布尔*停止){
ViewController*strongSelf=weakSelf;
如果(!result.缩略图)
返回;
[strongSelf.thumbnails添加对象:[UIImage imageWithCGImage:result.thumbnails]];
[strong自我更新];
}];
}failureBlock:^(NSError*error){}];
}
-(作废)更新
{
[self.collectionView-reloadData];
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
{
返回self.thumbnails.count;
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath
{
UIImage*image=self.thumbnails[indexPath.row];
返回图像大小;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
ImageCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“cell”forIndexPath:indexPath];
cell.imageView.image=self.thumbnails[indexPath.row];
返回单元;
}
@结束
此代码需要根据应用程序的要求从这里进行优化。它还需要自定义集合视图框架和布局,以符合UI要求
有一种方法可以模仿完全相同的行为:
你可以从这里下载:我通过使用组件
RRMessageController
解决了我自己的问题,我不知道Twitter是如何实现的(duh),但是你可以使用容器来实现这一点。只需在内存中创建UIImagePicker
,而不是使用presentViewController…
,只需将其添加为子视图控制器即可。我还没有测试过这种方法,所以在有人做评论之前我一直在做评论。这只是在点击按钮时显示它,我希望它默认显示在视图控制器中。更新UI以显示self.thumbnails
,这是什么意思,你能给我举个一般性的例子吗?正确地做这件事涉及到很多代码,但这是肯定的。
#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