2011-10-26 4 views
0

私はいくつかのオブジェクトのライブスキャンを行う必要があるiOSアプリケーションを開発しています。このためには、例では1秒間に3,4フレームを取る必要があります。ここでは、キャプチャセッションを作成するための私のコードは次のとおりです。カメラのライブスキャンiOS

// Create an AVCaptureSession 
    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; 
    captureSession.sessionPreset = AVCaptureSessionPresetHigh; 

    // Find a suitable AVCaptureDevice 
    AVCaptureDevice *photoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    // Create and add an AVCaptureDeviceInput 
    NSError *error = nil; 
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:photoCaptureDevice error:&error]; 
    if(videoInput){ 
     [captureSession addInput:videoInput]; 
    } 

    // Create and add an AVCaptureVideoDataOutput 
    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init]; 
    // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA' 
    NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject: 
             [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 
    [videoOutput setVideoSettings:rgbOutputSettings]; 

    // Configure your output, and start the session 
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL); 
    [videoOutput setSampleBufferDelegate:self queue:queue]; 

    if(videoOutput){ 
     [captureSession addOutput:videoOutput]; 
    } 

    [captureSession startRunning]; 


    // Setting up the preview layer for the camera 
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession]; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    previewLayer.frame = cameraViewCanvas.bounds; 

    // ADDING FINAL VIEW layer TO THE MAIN VIEW sublayer 
    [cameraViewCanvas.layer addSublayer:previewLayer]; 

とキューで呼び出されるデリゲートメソッド:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{ 
    if(isCapturing){ 
     NSLog(@"output"); 

     CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
     CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate); 
     CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(NSDictionary *)attachments]; 

     UIImage *newFrame = [[UIImage alloc] initWithCIImage:ciImage]; 
     [self showImage:newFrame]; 
    } 
} 

問題は、私は、画面上の画像、無エラーと警告を見ることができないということです、画像は表示されません。私の質問は - 私は正しい道のりで、画面上にイメージを表示するためにコード内で修正する必要があるのですか?

+0

あなたは 'cameraViewCanvas'についての詳細情報を提供しますか?どこに定義され、初期化されているかなど... – yinkou

+0

カメラのビデオ出力を表示するのは、@Operty(nonatomic、retain)IBOutlet UIView * cameraViewCanvas;です。他のオブジェクト、つまりUIImageViewを追加して、まだ取り込んだフレームをプレビューすることができます。 – Artem

+0

これをInterfaceBuilderにメインビューに追加しましたか? – yinkou

答えて

0

問題が発生している可能性がありますが、主スレッドで画像を設定していない可能性があります(captureOutputは、作成した別のディスパッチキューで呼び出されます)。

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self showImage:newFrame]; 
}); 

または

[self performSelectorOnMainThread:@selector(showImage:) newFrame waitUntilDone:YES]; 
関連する問題