gpt4 book ai didi

macos - 如何在 OS X 中将带有 JPEG 数据的 NSData 对象转换为 CVPixelBufferRef?

转载 作者:行者123 更新时间:2023-12-03 16:18:35 38 4
gpt4 key购买 nike

正如标题所提到的。

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

最佳答案

您可以使用以下方法将 NSImage 转换为 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;
}

生成的缓冲区可以通过 AVAssetWriterInputPixelBufferAdaptorappendPixelBuffer:: 附加到 AVAssetWriter

关于macos - 如何在 OS X 中将带有 JPEG 数据的 NSData 对象转换为 CVPixelBufferRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17481254/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com