2015-05-12 54 views
0

我正在努力調試一個奇怪的問題。在CVPixelBufferUnlockBaseAddress(imageBuffer,0);之後的captureOutput:didOutputSampleBuffer:fromConnection:整個UI停止響應觸摸。相機預覽工作,但我所有的按鈕停止響應,我甚至添加了一個UITapGesture,也不會工作。我試圖把它放入派遣,但仍然沒有成功。CVPixelBufferUnlockBaseAddress - Block UI

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    if (state != CAMERA) { 
     return; 
    } 

    if (self.state != CAMERA_DECODING) 
    { 
     self.state = CAMERA_DECODING; 
    } 


    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    //Lock the image buffer 
    CVPixelBufferLockBaseAddress(imageBuffer,0); 
    //Get information about the image 
    baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0); 

    int pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer); 
    switch (pixelFormat) { 
     case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: 

      //NSLog(@"Capture pixel format=NV12"); 
      bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0); 
      width = bytesPerRow;//CVPixelBufferGetWidthOfPlane(imageBuffer,0); 
      height = CVPixelBufferGetHeightOfPlane(imageBuffer,0); 
      break; 
     case kCVPixelFormatType_422YpCbCr8: 


      //NSLog(@"Capture pixel format=UYUY422"); 
      bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0); 
      width = CVPixelBufferGetWidth(imageBuffer); 
      height = CVPixelBufferGetHeight(imageBuffer); 
      int len = width*height; 
      int dstpos=1; 
      for (int i=0;i<len;i++){ 
       baseAddress[i]=baseAddress[dstpos]; 
       dstpos+=2; 
      } 

      break; 
     default: 
      // NSLog(@"Capture pixel format=RGB32"); 
      break; 
    } 

    unsigned char *pResult=NULL; 

    int resLength = MWB_scanGrayscaleImage(baseAddress,width,height, &pResult); 

    CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

回答

0

可能發生這種情況的原因是您正在主線程上運行所有操作。
針對您必須在另一隊列中運行的回調機會,就會議的輸出:

AVCaptureVideoDataOutput * dataOutput = [[AVCaptureVideoDataOutput alloc] init]; 
      [dataOutput setAlwaysDiscardsLateVideoFrames:YES]; 
      [dataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; 
dispatch_queue_t queue = dispatch_queue_create("it.CloudInTouchLabs.avsession", DISPATCH_QUEUE_SERIAL); 
      [dataOutput setSampleBufferDelegate:(id)self queue:queue]; 
      if ([captureSession_ canAddOutput:dataOutput]) { 
       [captureSession_ addOutput:dataOutput]; 
      } 

在此示例中,我創建一個串行隊列。

+0

這是問題所在。謝謝。 – Macaret