你如何使用AVCaptureStillImageOutput,captureStillImageAsynchronouslyFromConnection?

时间:2010-09-01 22:59:22

标签: iphone xcode avfoundation

如何为:

设置完成处理程序

captureStillImageAsynchronouslyFromConnection:completionHandler:

for AVCaptureStillImageOutput?

  • (void)captureDelegate:(CMSampleBufferRef)缓冲区错误:(NSError *)错误;

1 个答案:

答案 0 :(得分:17)

使用块。 像这样的东西:

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                     completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                         if (imageDataSampleBuffer != NULL) {
                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];                                                                 
                                                             ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                                                             [library writeImageToSavedPhotosAlbum:[image CGImage]
                                                                                       orientation:(ALAssetOrientation)[image imageOrientation]
                                                                                   completionBlock:^(NSURL *assetURL, NSError *error){
                                                                                       if (error) {
                                                                                           id delegate = [self delegate];
                                                                                           if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                                               [delegate captureStillImageFailedWithError:error];
                                                                                           }                                                                                               
                                                                                       }
                                                                                   }];
                                                             [library release];
                                                             [image release];
                                                         } else if (error) {
                                                             id delegate = [self delegate];
                                                             if ([delegate respondsToSelector:@selector(captureStillImageFailedWithError:)]) {
                                                                 [delegate captureStillImageFailedWithError:error];
                                                             }
                                                         }
                                                     }];