2013-10-18 42 views
0

我創建了一個攝像機視圖,通過創建AVCaptureVideoPreviewLayer來顯示在屏幕上,現在我希望它能夠顯示在我的滾動視圖中。目前我的滾動視圖設置爲處理UIImageViews,但不是相機層。以下是它們如何一起工作的:UIScrollView顯示攝像機

//設置攝像頭視圖 [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裏面,所以我已經註釋掉了CGRectlayerRect,而是試圖讓它在我的數組中顯示在scrollView上。在scrollView的底部,我有一個UIImageView正在採取數組並顯示它,但是這不會顯然工作,因爲相機不是圖像。我可以改變或研究底部做什麼工作?將其更改爲UIView?謝謝。

回答

1

完成循環後,只需在UIScrollView的底部創建並添加UIView(使用正確的位置和矩形)。然後,將視頻預覽圖層添加爲添加到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; 
} 
+0

嘿!感謝所以我編輯了代碼,並將其更改爲一個UIView和即時通訊關閉,但我得到錯誤''NSInvalidArgumentException',原因:' - [CaptureManager超級檢視]:任何想法? – Inovize

+0

請參閱編輯答案。 –

+0

這解決了superview問題,它工作沒有錯誤,但現在它不顯示任何東西,但從我viewController的白色屏幕。儘管我認爲它很接近! – Inovize

相關問題