等待 UIImage
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的示例来看,可以将 AVCamCaptureManager 传递给委托。
当它完成捕获图像后,它会在委托上调用 captureManagerStillImageCaptured: 。当委托方法被触发时,您可以完成这项工作,即
如果您不了解委托的工作原理,那么我建议在这里搜索或提出另一个问题。
委托教程
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