1

我正在快速开发一个 CLI 并在其中使用 FBSimulatorControl。我想流式传输从 consumeData: 委托方法获得的数据。根据方法中获取的数据是从 CVPixelBufferRef 获取的数据

我在 FBSimulatorControl 的 github 存储库中记录了一个问题。

我尝试从数据中重新生成 CVPixelBuffer 并尝试从中获取 CIImage 并将其转换为 jpeg 数据。但这似乎不起作用。谁能帮我这个?在我尝试过的代码下面添加

var pixelBuffer:CVPixelBuffer? = nil
let result = CVPixelBufferCreate(kCFAllocatorDefault, 750, 1334, kCVPixelFormatType_32BGRA, nil, &pixelBuffer)

if result != kCVReturnSuccess {
    print("pixel buffer create success")
    return
}
CVPixelBufferLockBaseAddress(pixelBuffer!, .init(rawValue: 0))
let yDestPlane:UnsafeMutableRawPointer? = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer!, 0)

if yDestPlane == nil {
    print("failed to create ydestplane")
    return
}

let nsData = data as NSData
let rawPtr = nsData.bytes

memcpy(yDestPlane!, rawPtr, 750*1334*4)

CVPixelBufferUnlockBaseAddress(pixelBuffer!, .init(rawValue: 0))
kCVPixelFormatType_32BGRA, getbasea, 2208, NULL, NULL, nil, &pixelBuffer)

if #available(OSX 10.12, *) {
    let ciImage = CIImage.init(cvPixelBuffer: pixelBuffer!)
    let tempContext = CIContext.init(options: nil)
    let videoImage = tempContext.createCGImage(ciImage, from: CGRect.init(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer!), height: CVPixelBufferGetHeight(pixelBuffer!)))
    let imageSize: NSSize = NSMakeSize(750, 1334)
    let nsImageTest = NSImage(cgImage: videoImage!, size: imageSize)
    if let bits = nsImageTest.representations.first as? NSBitmapImageRep {
        let jpegFinalData = bits.representation(using: .JPEG, properties: [:])
        if self.isStreaming {
            var simIndex:Int?
            for i in 0...(AUConnectionSimulatorMap.instance.simConnectionMap.count-1) {
                if sim.udid == AUConnectionSimulatorMap.instance.simConnectionMap[i].sim.sim.udid {
                    simIndex = i
                    break
                }
            }
            var finalData:Data = Data()
            let finalDict = ["data":["type":"onScreenFrame","value":jpegFinalData!]] as Dictionary<String,Any>
            try! finalData.pack(finalDict)
            AUConnectionSimulatorMap.instance.simConnectionMap[simIndex!].ws?.write(data: finalData)
        }

    }

} else {
    // Fallback on earlier versions
}
4

0 回答 0