2013-02-25 73 views
2

我試圖用一些蘋果source code的,但代碼中充斥着這些語句:蘋果源代碼 - 要求(錯誤==爲零,保釋)導致錯誤

require(error == nil, bail); 

這是造成各種在ARC項目中使用時出現錯誤(「轉到受保護的範圍」)。

我該如何解決這個問題?

下面是一個例子:

- (void)setupAVCapture 
{ 
    NSError *error = nil; 

    AVCaptureSession *session = [AVCaptureSession new]; 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
     [session setSessionPreset:AVCaptureSessionPreset640x480]; 
    else 
     [session setSessionPreset:AVCaptureSessionPresetPhoto]; 

    // Select a video device, make an input 
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 


    require(error == nil, bail); //ERROR: "Goto into protected scope" 

    isUsingFrontFacingCamera = NO; 
    if ([session canAddInput:deviceInput]) 
     [session addInput:deviceInput]; 

    // Make a still image output 
    self.stillImageOutput = [AVCaptureStillImageOutput new]; 
    [self.stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:(__bridge void *)(AVCaptureStillImageIsCapturingStillImageContext)]; 
    if ([session canAddOutput:self.stillImageOutput]) 
     [session addOutput:self.stillImageOutput]; 

    // Make a video data output 
    self.videoDataOutput = [AVCaptureVideoDataOutput new]; 

    // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA' 
    NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject: 
             [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 
    [self.videoDataOutput setVideoSettings:rgbOutputSettings]; 
    [self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image) 

    // create a serial dispatch queue used for the sample buffer delegate as well as when a still image is captured 
    // a serial dispatch queue must be used to guarantee that video frames will be delivered in order 
    // see the header doc for setSampleBufferDelegate:queue: for more information 
    videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL); 
    [self.videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue]; 

    if ([session canAddOutput:self.videoDataOutput]) 
     [session addOutput:self.videoDataOutput]; 
    [[self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:NO]; 

    effectiveScale = 1.0; 
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    [self.previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
    [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; 
    CALayer *rootLayer = [self.previewView layer]; 
    [rootLayer setMasksToBounds:YES]; 
    [self.previewLayer setFrame:[rootLayer bounds]]; 
    [rootLayer addSublayer:self.previewLayer]; 
    [session startRunning]; 

bail: 
    session = nil; 
    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed with error %d", (int)[error code]] 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Dismiss" 
                otherButtonTitles:nil]; 
     [alertView show]; 
     [self teardownAVCapture]; 
    } 

} 
+0

或者只是關閉ARC這些文件? – 2013-02-25 19:25:34

+0

@ H2CO3我無法單獨使用這些文件。我必須將部分複製並粘貼到我自己的代碼中。 – soleil 2013-02-25 19:26:48

+0

如果你想使用'require'調用,那麼你需要導入適當的頭文件:'#import '。 – rmaddy 2013-02-25 19:32:46

回答

5

添加打開和關閉括號提及的範圍

require(error == nil, bail); 
{ 

} 
bail: 
{ 

} 

例子:

require(error == nil, bail); 
    { 

     isUsingFrontFacingCamera = NO; 
     if ([session canAddInput:deviceInput]) 
      [session addInput:deviceInput]; 

     // Make a still image output 
     stillImageOutput = [AVCaptureStillImageOutput new]; 
     [stillImageOutput addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:(__bridge void *)(AVCaptureStillImageIsCapturingStillImageContext)]; 
     if ([session canAddOutput:stillImageOutput]) 
      [session addOutput:stillImageOutput]; 

     // Make a video data output 
     videoDataOutput = [AVCaptureVideoDataOutput new]; 

     // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA' 
     NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject: 
              [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 
     [videoDataOutput setVideoSettings:rgbOutputSettings]; 
     [videoDataOutput setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image) 

     // create a serial dispatch queue used for the sample buffer delegate as well as when a still image is captured 
     // a serial dispatch queue must be used to guarantee that video frames will be delivered in order 
     // see the header doc for setSampleBufferDelegate:queue: for more information 
     videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL); 
     [videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue]; 

     if ([session canAddOutput:videoDataOutput]) 
      [session addOutput:videoDataOutput]; 
     [[videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:NO]; 

     effectiveScale = 1.0; 
     previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
     [previewLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     [previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; 
     CALayer *rootLayer = [previewView layer]; 
     [rootLayer setMasksToBounds:YES]; 
     [previewLayer setFrame:[rootLayer bounds]]; 
     [rootLayer addSublayer:previewLayer]; 
     [session startRunning]; 
    } 

bail: 
    { 

     if (error) { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed with error %d", (int)[error code]] 
                   message:[error localizedDescription] 
                   delegate:nil 
                 cancelButtonTitle:@"Dismiss" 
                 otherButtonTitles:nil]; 
      [alertView show]; 

      [self teardownAVCapture]; 
     } 
    } 
+0

這不總是工作。顯然保釋是在一個或多個__block變量的範圍內;這是不允許的。有關更多信息,請參閱http://clang.llvm.org/compatibility.html#blocks-in-protected-scope。提出的解決方案是通過將__block變量的範圍放在花括號分隔的塊中來限制其範圍。這可能無法正常工作; – Rana 2014-05-15 05:38:29