我正在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
}