我目前正在实施一个应用程序,其中应该有两个摄像头视图作为两个页面。我使用scrollview来实现分页事件并且它成功了。我现在面临的问题是我无法一次添加两个摄像头视图。我只能放一个。有人可以帮助我。
这是我的屏幕
这是我的代码
#import "CameraViewController.h"
#import "WelcomeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "PhotoViewController.h"
@interface CameraViewController ()
@property (strong, nonatomic) UIWindow *window;
@end
@implementation CameraViewController{
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;
UIView *view_Photo;
UIView *view_Document;
UIScrollView *cameraScroll;
}
int page = 0;
float cameraScrollHeight;
- (void)viewDidLoad {
[super viewDidLoad];
cameraScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(X, Y, Width,Height)];
cameraScroll.pagingEnabled = YES;
cameraScroll.bounces = NO;
cameraScroll.showsHorizontalScrollIndicator = NO;
cameraScroll.showsVerticalScrollIndicator = NO;
cameraScroll.scrollsToTop = YES;
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//create the sub view and allocate memory
if (i == 0) {
view_Photo = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, ScrollViewHeight)];
view_Photo.backgroundColor = [UIColor clearColor];
}else{
view_Document = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, ScrollViewHeight)];
view_Document.backgroundColor = [UIColor clearColor];
}
//set the scroll view delegate to self so that we can listen for changes
cameraScroll.delegate = self;
//add the subview to the scroll view
if (i == 0) {
[cameraScroll addSubview:view_Photo];
}else{
[cameraScroll addSubview:view_Document];
}
}
//set the content size of the scroll view, we keep the height same so it will only
//scroll horizontally
cameraScroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,cameraScrollHeight);
//we set the origin to the 3rd page
CGPoint scrollPoint = CGPointMake(0, 0);
//change the scroll view offset the the 3rd page so it will start from there
[cameraScroll setContentOffset:scrollPoint animated:YES];
[self.view addSubview:cameraScroll];
}
//scrolling ends
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Scrolling - You are now on page %i",page);
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *) targetContentOffset
NS_AVAILABLE_IOS(5_0){
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Dragging - You are now on page %i",page);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
被修改
我忘记将这段代码放在上面的代码中。这个也在我的代码中。
- (void)viewWillAppear:(BOOL)animated{
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [cameraScroll];
[rootLayer setMasksToBounds:YES];
CGRect frame = view_Photo;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey,nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning];
}