如何使用AVCaptureStillImageOutput中的captureStillImageAsynchronouslyFromConnection方法?

7

如何为AVCaptureStillImageOutput的captureStillImageAsynchronouslyFromConnection:completionHandler:方法设置完成处理程序?

  • (void)captureDelegate:(CMSampleBufferRef)buffer error:(NSError *)error;
1个回答

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

你如何知道何时可以再次调用captureStillImageAsynchronouslyFromConnection? - Shizam
1
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 发生错误:+[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:] - 不是一个jpeg样本缓冲区。 - Rambatino

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接