【问题标题】:Is there a way creating image without pointer with CGContext in Swift 4有没有办法在 Swift 4 中使用 CGContext 创建没有指针的图像
【发布时间】:2019-05-17 12:19:21
【问题描述】:

通过使用 CGContext 和下面的代码,我们可以从缓冲区创建图像。 在这段代码中,您基本上指向内存中的起始地址,以及您将从内存中的该点获取数据以构造图像的距离。

但是,如果我们有一个变量来保存图像数据,让我们说 imageData 作为我们从其他来源(例如本地网络中的其他设备)下载的数据类型。

        let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer) 
        let colorSpace  = CGColorSpaceCreateDeviceRGB()     
        var bitmapInfo  = CGBitmapInfo.byteOrder32Little.rawValue
        bitmapInfo |= CGImageAlphaInfo.premultipliedFirst.rawValue 
        //CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

        let context = CGContext(data: baseAddress, width: videoWidth, height: videoHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)!

        let frameImage =   context.makeImage()!  // This is a CGImage

【问题讨论】:

  • 你看过CGDataProvider吗?

标签: ios swift


【解决方案1】:

假设您已经有一个名为data 的变量,它将图像数据保持为Data 类型,那么您可以使用此变量创建图像:

var image = UIImage(data: data)

【讨论】:

  • 我们有数据类型的数据来自网络,它是由CMSampleBufferGetImageBuffer类型或CMSampleBuffer类型转换而来的。
  • 我们有数据类型来自网络,它是由CMSampleBufferGetImageBuffer类型或CMSampleBuffer类型转换而来的。我做了你写的,首先从 UIImage 创建图像数据并通过网络发送它并在另一端重建,但我试图减少我想发送缓冲区数据而不需要任何额外进程的进程。因为双方都知道视频格式、大小比特等。在转换内存和 cpu 处理时间后,每帧发送它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 2013-08-22
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多