FBSimulatorControl:将CVPixelBufferRef数据转换为jpeg数据以进行流式传输

时间:2017-08-18 07:14:59

标签: swift macos uiimagejpegrepresentation cvpixelbuffer

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

获得的数据

我在FBSimulatorControl的github repo中记录了issue

我尝试从数据中重新生成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
}

0 个答案:

没有答案