0

だから私は写真を撮るために使用している AVCamViewController を持っています。背景はライブ画像フィードで、その上に写真を撮るためのボタンと撮影した写真を表示するための UIImageView がオーバーレイされています。これらは両方とも機能します。撮影したすべての写真を表示する横スクロール collectionView が必要ですが、ボタンと UIImageView の追加は問題ありませんでしたが、collectionView をオーバーレイ ビューに追加できないようです。以下は、View Controller の view did load メソッドの実装コードです。

- (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 が表示されない理由はありますか?

これが私のヘッダーファイルです:

#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 の実装ファイルで呼び出しているデリゲート メソッドは次のとおりです。

- (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;
    }

これらのデリゲート メソッドはどちらも呼び出されることはありません。どんな助けでも大歓迎です!ありがとう!

4

1 に答える 1

0

collectionView が @property である理由はありますか? 次のようなものを実装してみませんか:

@interface AVOverlayViewController : UIViewController <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *collectionView;
}

それでも解決しない場合は、(viewDidLoad でサイズを設定する代わりに)(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPathデリゲートで - を実装してみてください。

また、小さく始めて、最初にセルをロードして、それが機能するかどうかを確認することもお勧めします。コレクションが適切に実装されていることを確認したら、必要な画像を追加します。空白の緑色のセルをロードすることから始めて、コレクション ビューの背景色を目立つ色に設定します。

于 2013-11-07T04:02:27.640 に答える