无法使用 MTKTextureLoader 将大型 jpeg 加载到 MTLTexture 中

亲和力

我正在尝试将大图像加载到 MTLTexture 中,它可以处理 4000x6000 图像。但是当我尝试使用 6000x8000 时它不能。

func setTexture(device: MTLDevice, imageName: String) -> MTLTexture? { let textureLoader = MTKTextureLoader(device: device)

    var texture: MTLTexture? = nil

    //  In iOS 10 the origin was changed.
    let textureLoaderOptions: [MTKTextureLoader.Option: Any]
    if #available(iOS 10.0, *) {
        let origin = MTKTextureLoader.Origin.bottomLeft.rawValue
        textureLoaderOptions = [MTKTextureLoader.Option.origin : origin]
    } else {
        textureLoaderOptions = [:]
    }

    if let textureURL = Bundle.main.url(forResource: imageName, withExtension: nil, subdirectory: "Images") {
        do {
            texture = try textureLoader.newTexture(URL: textureURL, options: textureLoaderOptions)
        } catch {
            print("Texture not created.")
        }
    }
    return texture
}

非常基本的代码。我在配备 A9 芯片、GPU 系列 3 的 iPad Pro 上运行它。它应该可以处理这么大的纹理。如果它不接受这个尺寸,我应该以某种方式手动平铺它吗?在这种情况下,最好的方法是什么:使用 MTLRegionMake 复制字节,在 Core Image 或 Core Graphics 上下文中切片......

我感谢任何帮助

亲和力

根据您的有用评论,我决定手动将其加载到 CGContext 并复制到 MTLTexture。我正在添加下面的解决方案代码。不应在每次创建纹理时都创建上下文,最好将其放在函数之外并继续重用它。

    // Grab the CGImage, w = width, h = height...

    let context = CGContext(data: nil, width: w, height: h, bitsPerComponent: bpc, bytesPerRow: (bpp / 8) * w, space: colorSpace!, bitmapInfo: bitmapInfo.rawValue)

    let flip = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(h))
    context?.concatenate(flip)
    context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(w), height: CGFloat(h)))

    let textureDescriptor = MTLTextureDescriptor()
    textureDescriptor.pixelFormat = .rgba8Unorm
    textureDescriptor.width = w
    textureDescriptor.height = h

    guard let data = context?.data else {print("No data in context."); return nil}

    let texture = device.makeTexture(descriptor: textureDescriptor)
    texture?.replace(region: MTLRegionMake2D(0, 0, w, h), mipmapLevel: 0, withBytes: data, bytesPerRow: 4 * w)

    return texture

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 MTKTextureLoader 加载远程图像

来自分类Dev

SSIS使用太多内存无法将大型(40GB +)XML文件加载到SQL Server表中

来自分类Dev

如何使用打字稿将数组动态加载到大型菜单的列中?

来自分类Dev

无法将js加载到MongoDB中

来自分类Dev

无法将脚本加载到iframe中

来自分类Dev

无法使用angularJS将网址加载到iframe中

来自分类Dev

将大型SAS数据加载到R / Python中

来自分类Dev

Python ETL-使用cx_Oracle批量或迭代将大型数据集加载到Oracle数据库中

来自分类Dev

无法将HTML中的AJAX加载到div中

来自分类Dev

无法将GCS中的CSV文件加载到bigquery中

来自分类Dev

无法将示例文件加载到hadoop 2.2.0中

来自分类Dev

无法将简单的网格物体加载到OpenGL中

来自分类Dev

无法将大图像加载到Web View Android中

来自分类Dev

无法将数据加载到javafx表格中

来自分类Dev

无法将数据加载到配置单元表中

来自分类Dev

QDialog:无法从用户输入将数据加载到QTableWidget中

来自分类Dev

无法将数据加载到Pig中的Hortonworks沙箱

来自分类Dev

无法将ArrayList数据加载到Android中的微调器

来自分类Dev

无法将简单的网格物体加载到OpenGL中

来自分类Dev

无法将JavaScript文件加载到Django应用中

来自分类Dev

ember JSONAPIAdapter无法将数据正确加载到存储中

来自分类Dev

无法将更新的列表重新加载到datagridview中

来自分类Dev

无法将数据加载到javafx表格中

来自分类Dev

无法将特定图块的“邻居”加载到列表中

来自分类Dev

无法将大图像加载到Web View Android中

来自分类Dev

无法将js函数加载到html中

来自分类Dev

无法将CSV数据加载到GrapheneDB实例中

来自分类Dev

无法将CSS文件加载到Django 1.10中

来自分类Dev

无法将本地jar加载到我的jar中

Related 相关文章

  1. 1

    使用 MTKTextureLoader 加载远程图像

  2. 2

    SSIS使用太多内存无法将大型(40GB +)XML文件加载到SQL Server表中

  3. 3

    如何使用打字稿将数组动态加载到大型菜单的列中?

  4. 4

    无法将js加载到MongoDB中

  5. 5

    无法将脚本加载到iframe中

  6. 6

    无法使用angularJS将网址加载到iframe中

  7. 7

    将大型SAS数据加载到R / Python中

  8. 8

    Python ETL-使用cx_Oracle批量或迭代将大型数据集加载到Oracle数据库中

  9. 9

    无法将HTML中的AJAX加载到div中

  10. 10

    无法将GCS中的CSV文件加载到bigquery中

  11. 11

    无法将示例文件加载到hadoop 2.2.0中

  12. 12

    无法将简单的网格物体加载到OpenGL中

  13. 13

    无法将大图像加载到Web View Android中

  14. 14

    无法将数据加载到javafx表格中

  15. 15

    无法将数据加载到配置单元表中

  16. 16

    QDialog:无法从用户输入将数据加载到QTableWidget中

  17. 17

    无法将数据加载到Pig中的Hortonworks沙箱

  18. 18

    无法将ArrayList数据加载到Android中的微调器

  19. 19

    无法将简单的网格物体加载到OpenGL中

  20. 20

    无法将JavaScript文件加载到Django应用中

  21. 21

    ember JSONAPIAdapter无法将数据正确加载到存储中

  22. 22

    无法将更新的列表重新加载到datagridview中

  23. 23

    无法将数据加载到javafx表格中

  24. 24

    无法将特定图块的“邻居”加载到列表中

  25. 25

    无法将大图像加载到Web View Android中

  26. 26

    无法将js函数加载到html中

  27. 27

    无法将CSV数据加载到GrapheneDB实例中

  28. 28

    无法将CSS文件加载到Django 1.10中

  29. 29

    无法将本地jar加载到我的jar中

热门标签

归档