修改 CVPixelBuffer

Posted

技术标签:

【中文标题】修改 CVPixelBuffer【英文标题】:Modifying CVPixelBuffer 【发布时间】:2021-11-19 01:57:00 【问题描述】:

我正在使用下面的方法将绘图添加到像素缓冲区,然后将其附加到AVAssetWriterInputPixelBufferAdaptor

它适用于我的 Mac mini (macOS 12 beta 7),但 drawingHandler 在我的 MacBook (macOS 11.5.2) 上没有任何效果。

这段代码有什么问题吗?

#if os(macOS)
import AppKit
#else
import UIKit
#endif

import CoreMedia

extension CMSampleBuffer 
    
    func pixelBuffer(drawingHandler: ((CGRect) -> Void)? = nil) -> CVPixelBuffer? 
        guard let pixelBuffer = CMSampleBufferGetImageBuffer(self) else 
            return nil
        
        guard let drawingHandler = drawingHandler else 
            return pixelBuffer
        
        guard CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) == kCVReturnSuccess else 
            return pixelBuffer
        
        
        let data = CVPixelBufferGetBaseAddress(pixelBuffer)
        let width = CVPixelBufferGetWidth(pixelBuffer)
        let height = CVPixelBufferGetHeight(pixelBuffer)
        let bitsPerComponent = 8
        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let imageByteOrderInfo = CGImageByteOrderInfo.order32Little
        let imageAlphaInfo = CGImageAlphaInfo.premultipliedFirst
        
        if let ctx = CGContext(data: data,
                               width: width,
                               height: height,
                               bitsPerComponent: bitsPerComponent,
                               bytesPerRow: bytesPerRow,
                               space: colorSpace,
                               bitmapInfo: imageByteOrderInfo.rawValue | imageAlphaInfo.rawValue)
        
            // Push
            #if os(macOS)
            let graphCtx = NSGraphicsContext(cgContext: ctx, flipped: false)
            NSGraphicsContext.saveGraphicsState()
            NSGraphicsContext.current = graphCtx
            #else
            UIGraphicsPushContext(ctx)
            #endif
            
            let rect = CGRect(x: 0, y: 0, width: width, height: height)
            drawingHandler(rect)
            
            // Pop
            #if os(macOS)
            NSGraphicsContext.restoreGraphicsState()
            #else
            UIGraphicsPopContext()
            #endif
        
        
        CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
        
        return pixelBuffer
    
    

【问题讨论】:

【参考方案1】:

更改锁定标志。

let lockFlags = CVPixelBufferLockFlags(rawValue: 0)

guard CVPixelBufferLockBaseAddress(pixelBuffer, lockFlags) == kCVReturnSuccess else 
    return pixelBuffer


// ...

CVPixelBufferUnlockBaseAddress(pixelBuffer, lockFlags)

【讨论】:

以上是关于修改 CVPixelBuffer的主要内容,如果未能解决你的问题,请参考以下文章

怎么修改文件修改日期

电脑文件修改时间和实际修改时间不一样

ce修改器使用方法

Sql Server 增加字段修改字段修改类型修改默认值(转)

网卡MAC怎么修改

关于windows 2003域控账户修改密码问题,客户端账号过期的时候修改密码提示“您无权修改密码”。