本文介绍了如何将NSData对象与JPEG数据转换为CVPixelBufferRef在OS X?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所提到的。

我用Google和Stackoverflow搜索,所有资源都用于UIImage转换,或者将NSImage从CVPixelBufferRef转换。现在我想做的是将JPEG原始数据转换为CVPixelBufferRef,以便我可以生成一个带有现场jpeg流的电影文件。

I search with Google and Stackoverflow, all resources are for UIImage converting, or convert NSImage FROM CVPixelBufferRef. Now what I want to do is convert JPEG raw data TO CVPixelBufferRef so that I could generate a movie file with live jpeg streams.

推荐答案

您可以使用以下方法将 NSImage 转换为 CVPixelBufferRef

You can use the following method to convert from NSImage to CVPixelBufferRef:

- (CVPixelBufferRef)newPixelBufferFromNSImage:(NSImage*)image
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    NSDictionary* pixelBufferProperties = @{(id)kCVPixelBufferCGImageCompatibilityKey:@YES, (id)kCVPixelBufferCGBitmapContextCompatibilityKey:@YES};
    CVPixelBufferRef pixelBuffer = NULL;
    CVPixelBufferCreate(kCFAllocatorDefault, [image size].width, [image size].height, k32ARGBPixelFormat, (__bridge CFDictionaryRef)pixelBufferProperties, &pixelBuffer);
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    void* baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
    CGContextRef context = CGBitmapContextCreate(baseAddress, [image size].width, [image size].height, 8, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst);
    NSGraphicsContext* imageContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:imageContext];
    [image compositeToPoint:NSMakePoint(0.0, 0.0) operation:NSCompositeCopy];
    [NSGraphicsContext restoreGraphicsState];
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    CFRelease(context);
    CGColorSpaceRelease(colorSpace);
    return pixelBuffer;
}

生成的缓冲区可以附加到 AVAssetWriter via AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer ::

The resulting buffer can be appended to an AVAssetWriter via AVAssetWriterInputPixelBufferAdaptor's appendPixelBuffer::

这篇关于如何将NSData对象与JPEG数据转换为CVPixelBufferRef在OS X?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:12