等待 UIImage

发布于 2024-12-09 01:17:26 字数 3597 浏览 0 评论 0原文

我在 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 中有其他方法;我该怎么办才能解决这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-12-16 01:17:26

从您的示例来看,可以将 AVCamCaptureManager 传递给委托。
当它完成捕获图像后,它会在委托上调用 captureManagerStillImageCaptured: 。当委托方法被触发时,您可以完成这项工作,即

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

}

如果您不了解委托的工作原理,那么我建议在这里搜索或提出另一个问题。

委托教程

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.

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

}

If you do not understand how delegates work then i suggest searching on here or asking another question.

Delegate Tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文