Жди UIImage - PullRequest
       5

Жди UIImage

0 голосов
/ 06 октября 2011

У меня есть этот код в AVCamCaptureManager:

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

и этот метод в другом классе

- (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, у меня когда-либо будет image = nil, потому что у меня есть другой метод в AsynchronouslyFromConnection; что я могу сделать, чтобы решить эту ситуацию?

1 Ответ

1 голос
/ 06 октября 2011

Судя по вашему примеру, AVCamCaptureManager может быть передан делегату.Когда он завершает захват изображения, он вызывает captureManagerStillImageCaptured: для делегата.Когда метод делегатов запущен, вы можете выполнить эту работу, например:

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

}

Если вы не понимаете, как работают делегаты, тогда я предлагаю поискать здесь или задать другой вопрос.

ДелегатУчебное пособие

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...