0
我想使用AVCaptureSession製作相機應用程序。現在我只想看看視頻輸入是否有效。但它看起來沒有任何輸入,我似乎無法理解爲什麼。未找到AVCapture會話。無法添加視頻輸入
- (void)viewDidLoad
{
[super viewDidLoad];
session = [[AVCaptureSession alloc] init];
[self addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[self previewLayer] setBounds:layerRect];
[[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[self previewLayer]];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(80, 320, 200, 44);
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:myButton];
}
-(void)addVideoPreviewLayer
{
[self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self session]] autorelease]];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
}
-(void) addVideoInput
{
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice)
{
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error)
{
if ([[self session] canAddInput:videoIn])
[[self session] addInput:videoIn];
else
NSLog(@"Couldn't add video input");
}
else
NSLog(@"Couldn't create video input");
}
else
NSLog(@"Couldn't create video capture device");
}
-(IBAction)scanButtonPressed
{
[self addVideoInput];
}
此代碼的結果(控制檯輸出)是什麼? – Till 2012-01-06 21:32:57
@Till無法添加視頻輸入。 – ilaunchpad 2012-01-06 21:43:40