ios - CVPixelBuffer 到 CGImage 并按比例缩小
问题描述
我有以下代码要转换CVPixelBuffer
,CGImage
它工作正常:
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
// Get the number of bytes per row for the pixel buffer
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!)
// Get the number of bytes per row for the pixel buffer
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
// Get the pixel buffer width and height
let width = CVPixelBufferGetWidth(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
// Create a device-dependent RGB color space
let colorSpace = CGColorSpaceCreateDeviceRGB()
// Create a bitmap graphics context with the sample buffer data
let bitmap = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Little.rawValue|CGImageAlphaInfo.premultipliedFirst.rawValue)
let contextBM = CGContext(data: baseAddress, width: width, height: height, bitsPerComponent: 8,
bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmap.rawValue)
// Create a Quartz image from the pixel data in the bitmap graphics context
let quartzImage = contextBM!.makeImage()
//Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
self.myCGImage = quartzImage
现在,我希望CGImage
创建的尺寸更小。也许一半大小?是否可以在不运行任何额外内容的情况下修改上述代码?
解决方案
推荐阅读
- python - 还有其他方法可以解决whatsapp消息的自动化吗?
- r - 如何调整热图以显示所有内容?
- google-cloud-platform - 气流 1.10.15。似乎无缘无故杀死僵尸任务
- docker - Docker中孤立美元符号的含义
- visual-studio - 如何重置 IIS Express SSL 配置
- docker - Shadow SLJS 未通过 Docker 挂载检测文件更改
- api - Coturn:限时凭证和转秘密问题?
- java - 每次将输入值与数组中的元素匹配
- javascript - 如何通过单击单选按钮更改范围滑块添加?
- spring-boot - 在 Web 浏览器上的 jsp 文件只打印它的名称。它不会打印正文标签中的字符串内容。我尝试了很多方法,但对我没有任何作用