0

AVCaptureSessionを使用してカメラアプリを作成しようとしています。今のところ、ビデオ入力が機能するかどうかを確認したいだけです。しかし、入力がないようで、理由がわからないようです。

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

1 に答える 1