【发布时间】:2017-02-06 13:57:02
【问题描述】:
如何在 Swift 3 中将 captureStillImageAsynchronously(sampleBuffer) 转换为 base64 编码
我正在尝试将图像数据连续提供给 HTML Web 视图,在发现之后,没有 Cordova 插件可以满足这个目标,因为我需要在画布上绘制图像/视频数据,以便我可以使用 js 库对其进行处理。
最初我想使用 WEBRTC,但它仅适用于 android。我正在 IOS 9+ 上开发。
因此,我正在尝试创建一个非常简单的插件,它可以实时显示预览并每 0.3 秒提供一次图像数据(使用 javascript Cordova 插件命令调用),以便处理图像。
获取sampleBuffer的快速教程: http://drivecurrent.com/using-swift-and-avfoundation-to-create-a-custom-camera-view-for-an-ios-app/
我需要的是将 captureStillImageAsynchronously sampleBuffer 转换为 base64 编码,或者是否有任何其他方式可以获得可以通过 javascript 处理的图像数据?
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData as! CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
self.capturedImage.image = image
print(sampleBuffer) // how to convert this to 64encode ? without saving to camera roll
}
})
}
【问题讨论】:
-
请注意,函数
captureStillImageAsynchronously(from:completionHandler:)在 iOS 10 中已被弃用。不幸的是,文档不喜欢替换方法。使用短语“captureStillImageAsynchronously deprecated”进行搜索表明我们应该改用AVCapturePhotoOutput类。 -
这个问题不应该有javascript标签。
-
我明白了,我现在删除了 javascript 标签