2011-10-06 70 views
0

我有这样的代码在AVCamCaptureManager:等待的UIImage

- (void) captureStillImage 
{ 
AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; 
if ([stillImageConnection isVideoOrientationSupported]) 
    [stillImageConnection setVideoOrientation:orientation]; 

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection 
                completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

                 ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { 
                  if (error) { 
                   if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { 
                    [[self delegate] captureManager:self didFailWithError:error]; 
                    } 
                  } 
                 }; 

                 if (imageDataSampleBuffer != NULL) { 
                  NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
                  //ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

                  //UIImage *imagePhoto = [[UIImage alloc] initWithData:imageData]; 
                  /* 
                  [library writeImageToSavedPhotosAlbum:[image CGImage] 
                         orientation:(ALAssetOrientation)[image imageOrientation] 
                        completionBlock:completionBlock];*/ 
                  self.image = [[UIImage alloc] initWithData:imageData]; 

                  //[imagePhoto release]; 

                  //[library release]; 
                 } 
                 else 
                  completionBlock(nil, error); 

                 if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) { 
                  [[self delegate] captureManagerStillImageCaptured:self]; 
                 } 
                }]; 
} 

这methos另一个类

- (IBAction)captureStillImage:(id)sender 
{ 
// Capture a still image 
//[[self stillButton] setEnabled:NO]; 
[[self captureManager] captureStillImage]; 

if ([captureManager image] == nil) NSLog(@"image nil"); 

[preview setImage:[captureManager image]]; 
[snap setAlpha:0.00]; 
[use setAlpha:1.00]; 
[retake setAlpha:1.00]; 

我的问题是,当我致电IBAction为我曾经像=零,因为我有另一种方法在AsynchronouslyFromConnection;我能做些什么来解决这种情况?

回答

1

从您的示例的外观可以传递AVCamCaptureManager委托。 当它完成捕获图像时,它会在代理上调用captureManagerStillImageCaptured:。当代表方法被触发,你可以做的工作,即

-(void)captureManagerStillImageCaptured:(id)sender 
{ 
[preview setImage:[captureManager image]]; 
[snap setAlpha:0.00]; 
[use setAlpha:1.00]; 
[retake setAlpha:1.00]; 

} 

如果你不明白的代表是如何工作的话,我建议在这里寻找或问另一个问题。

Delegate Tutorial