UIScrollView显示相机

时间:2013-10-18 07:06:10

标签: ios objective-c avcapturesession

我创建了一个摄像机视图,通过创建AVCaptureVideoPreviewLayer来显示在屏幕上,现在我希望它能够显示在我的滚动视图中。目前我的滚动视图设置为处理UIImageViews而不是相机层。以下是它们如何协同工作:

//设置摄像机视图     [self setCaptureManager:[[CaptureManager alloc] init]];

[[self captureManager] addVideoInput];

[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:layerRect];
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                              CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

[[captureManager captureSession] startRunning];


int PageCount = 2;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:settingsView,captureManager,nil];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
[scroller setShowsHorizontalScrollIndicator:NO];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
scroller.delegate = self;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
    UIView *view2 = [arrImageName objectAtIndex:i];
    [view1 addSubview:view2];
    [scroller addSubview:view1];
    scroller.contentSize = CGSizeMake(width, 0);
    width +=scroller.frame.size.width;
    xPos  +=scroller.frame.size.width;
}

捕获管理器处理AVCaptureVideoPreviewLayer,这一切都可以正常工作并自行显示。我试图让它显示在我的scrollView内,所以我已对CGRect layerRect进行了评论,而是尝试将其显示在scrollView的数组中。在scrollView的底部,我有一个UIImageView正在接收数组并显示它,但是这显然不会起作用,因为相机不是图像。我可以更改或深入了解底部是为了使其工作?将其更改为UIView?谢谢。

1 个答案:

答案 0 :(得分:1)

完成循环后,只需在UIView的底部创建并添加UIScrollView(具有正确的位置和矩形)。然后,将视频预览图层添加为添加到scrollView的视图的子图层。祝你好运!

修改 好的,试试这个:

[[self captureManager] addVideoInput];

[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:layerRect];
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                              CGRectGetMidY(layerRect))];
UIView *captureView = [[UIView alloc] initWithFrame:self.view.bounds];
[[captureView layer] addSublayer:[[self captureManager] previewLayer]];

[[captureManager captureSession] startRunning];


int PageCount = 2;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:settingsView,captureView,nil];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
[scroller setShowsHorizontalScrollIndicator:NO];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
scroller.delegate = self;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
    UIView *view2 = [arrImageName objectAtIndex:i];
    [view1 addSubview:view2];
    [scroller addSubview:view1];
    scroller.contentSize = CGSizeMake(width, 0);
    width +=scroller.frame.size.width;
    xPos  +=scroller.frame.size.width;
}