I have this code in 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];
}
}];
}
and this methos in another class
- (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];
my problem is that when I call the IBAction I have ever image = nil because I have the other method in AsynchronouslyFromConnection; what can I do to solve this situation?
From the looks of your example the AVCamCaptureManager can be passed a delegate.
When it has finished capturing the image it calls captureManagerStillImageCaptured: on the delegate. When the delegates method is fired you can do that work i.e.
If you do not understand how delegates work then i suggest searching on here or asking another question.
Delegate Tutorial