从CMSampleBuffer转换为UIImage对象

时间:2011-04-13 06:03:31

标签: iphone objective-c ios uiimage avfoundation

我尝试了一个示例,用于从相机捕获视频帧作为使用AV Foundation的图像,在文档中给出,即

http://developer.apple.com/library/ios/#qa/qa1702/_index.html

但是在委托方法中从CMSampleBufferRef获取UIImage对象的方法没有建立。

意味着我也导入了AVFoundation框架,但它提供了14个错误,例如_CVPixelBufferUnlockBaseAddress,引自:。

如果有人知道如何解决,请帮帮我。

提前致谢。如果有人知道,请告诉我。

4 个答案:

答案 0 :(得分:2)

这是从捕获的数据中捕获并获取图像的代码:

-(IBAction)startCapture
{
    //session object
    captureSession = [[AVCaptureSession alloc]init];
    captureSession.sessionPreset = AVCaptureSessionPresetMedium;

    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    previewLayer.frame = CGRectMake(0, 10, 320, 200); ////self.view.frame; //
    [self.view.layer addSublayer:previewLayer];

    NSError *error = nil;
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //input object
    AVCaptureDeviceInput *inputDevice = [[AVCaptureDeviceInput alloc]initWithDevice:device error:&error];
    [captureSession addInput:inputDevice];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [captureSession addOutput:stillImageOutput];
    [captureSession startRunning];
}

-(IBAction) captureNow
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) { break; }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);

         NSLog(@"exif Attachments:%@",exifAttachments);
         if (exifAttachments)
         {
             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];

             UIImage *image = [[UIImage alloc] initWithData:imageData];
             self.vImage.image = image;
             // Do something with the attachments.

         }
         else
             NSLog(@"no attachments");   
     }];
}

答案 1 :(得分:0)

您的最终目标是什么 - 您是否尝试将视频录制到视频文件中。或者只捕获特定的帧?我有一个使用AVCaptureSession捕获带有音频的视频文件的工作示例,如果它可以帮助我发布一些代码片段 - 但看到有一些位和涉及bobs我想知道你想要做什么。

干杯,

迈克尔

答案 2 :(得分:0)

一旦assetWriter完成录制动画/捕捉图像,就保存到库中。

将它放在执行文件上方的顶部*这是保存视频文件的示例,但您可以更改它以保存图像:

ALAssetsLibraryWriteVideoCompletionBlock _videoCompblock = ^(NSURL *assetURL, NSError *error){
if(assetURL){
    NSLog(@"Saved to camera roll with Video AssetUrl : %@", [assetURL absoluteString]);
    NSError *error;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:[assetURL absoluteString] error:&error];

    if (attributes){
        long fileSize = [[attributes objectForKey:NSFileSize] longValue];// unsigned long long
        NSLog(@"%d", fileSize);
    }

}else if(error){
    NSLog(@"The Error occured : %@", [error localizedDescription]);
}

};

然后你需要一个使用上面的块的函数 - 所以当你捕获会话结束时,录制有类似的东西:

-(void) stopRecording{
writing = NO;
isRecording = NO;
[audioInput markAsFinished];//if you have an audio writer stop it too
[videoInput markAsFinished];
[assetWriter endSessionAtSourceTime:[frTimer currentTimeStamp]];
[assetWriter finishWriting];

finished = YES;

[videoUtilities saveToCamera:[assetWriter outputURL]];
NSLog(@"%@", [[assetWriter outputURL] absoluteString]);

}

触发保存到相机功能,看起来有点像这样:

+(void) saveToCamera:(NSURL *)urlPath{

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

AVAsset *movFile = [AVURLAsset URLAssetWithURL:urlPath options:nil];
NSLog(@"Movie File %@", movFile);

BOOL isSupported = [library videoAtPathIsCompatibleWithSavedPhotosAlbum:urlPath];

if(isSupported){
    NSLog(@"IS SUPPORTED - SAVING TO CAMERA ROLL");
    [library writeVideoAtPathToSavedPhotosAlbum:urlPath completionBlock:_videoCompblock];

}

}

如果您尝试在拍摄时实时显示保存的图像 - 您需要在拍摄照片时将UIImageData复制到UIImage对象 - 并将其指定为UIImageView的图像。或者你可以通过assetLibrary枚举并从那里拉出来。

希望这有帮助,

干杯,

迈克尔

答案 3 :(得分:0)

您必须导入CoreMedia.framework和CoreVideo.framework