首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向AVCam添加UICollectionView覆盖

向AVCam添加UICollectionView覆盖
EN

Stack Overflow用户
提问于 2013-11-07 09:07:22
回答 1查看 623关注 0票数 0

所以我有一个用来拍照的AVCamViewController。背景是实时图像馈送,覆盖在顶部的是用于拍摄照片的按钮和用于显示拍摄的照片的UIImageView。这两种方法都有效。我想有一个侧滚collectionView来显示所有拍摄的照片,但我似乎不能添加collectionView到覆盖视图,即使添加按钮和UIImageView没有问题。以下是视图控制器的视图did加载方法的实现代码:

代码语言:javascript
复制
- (void)viewDidLoad {


_imagesTaken = [[NSMutableArray alloc] init];

_vImagePreview = [[UIView alloc] initWithFrame:self.view.bounds];
_vImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];

UIButton *shutter = [[UIButton alloc] initWithFrame:CGRectMake(100,500,50,50)];
[shutter addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
[shutter setBackgroundColor:[UIColor greenColor]];

[_vImagePreview addSubview:shutter];
[_vImagePreview addSubview:_vImage];



UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setItemSize:CGSizeMake(50, 50)];

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 400, 315, 60) collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor redColor];
_collectionView.pagingEnabled = YES;
_collectionView.delegate = self;
_collectionView.dataSource = self;
//[_collectionView setDataSource:self];
//[_collectionView setDelegate:self];

[_collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

[_vImagePreview addSubview:_collectionView];
[_collectionView reloadData];

[self.view addSubview:_vImagePreview];

[super viewDidLoad];

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

//CALayer *viewLayer = self.vImagePreview.layer;
//NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[_stillImageOutput setOutputSettings:outputSettings];
[session addOutput:_stillImageOutput];

[session startRunning];

}

你知道为什么按钮和UIImageView会出现,而collectionView却没有吗?

下面是我的头文件:

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "CaptureSessionManager.h"

@interface AVOverlayViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (retain) CaptureSessionManager *captureManager;
@property (nonatomic, strong) UILabel *scanningLabel;
@property (nonatomic, strong) NSMutableArray *imagesTaken;

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
@property (nonatomic, retain) IBOutlet UICollectionView *collectionView;



@end

另外,这些是我在ViewController的实现文件中调用的委托方法:

代码语言:javascript
复制
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_imagesTaken count];
    NSLog(@"%d added to collection view",[_imagesTaken count]);
}


    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        PhotoCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCellIdentifier" forIndexPath:indexPath];

        UIImage *fromCamera = [_imagesTaken objectAtIndex:indexPath.row];

        [[cell imageView] setImage:fromCamera];

        cell.backgroundColor=[UIColor greenColor];

        return cell;
    }

这两个委托方法都不会被调用。任何帮助都是非常感谢的!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-11-07 12:02:28

collectionView是@属性有什么原因吗?为什么不实现像这样的东西:

代码语言:javascript
复制
@interface AVOverlayViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *collectionView;
}

如果这样还不能解决问题,您可以尝试(而不是在viewDidLoad中设置大小)在委托中实现- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

我也会说从小的开始,先加载细胞,看看你是否能让它工作。然后,一旦您确定集合已正确实现,就可以添加所需的图像。我会从加载空白的绿色单元格开始,然后可能会将集合视图的背景颜色设置为一些明显的颜色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19826091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档