I tried it with the following code with a larger image:
import ModelIO
if #available(macOS 11.0, *) {
var floatData = UnsafeMutableBufferPointer<Float32>.allocate(capacity: 16*16)
for i in 0..<256{
if (i%2 == 0){
floatData[i] = Float32(0.99)
} else {
floatData[i] = Float32(0)
}
}
let floatBuffer = Data(buffer: floatData)
let exrImg = MDLTexture(data: floatBuffer,
topLeftOrigin: true,
name: "output.exr",
dimensions: SIMD2<Int32>(16,16),
rowStride: 16 * MemoryLayout<Float32>.size,
channelCount: 1,
channelEncoding: MDLTextureChannelEncoding.float32,
isCube: false)
let resourcesURL = URL(fileURLWithPath: "/Users/myDirectory", isDirectory: true)
let fileName = resourcesURL.appendingPathComponent(exrImg.name)
if(exrImg.write(to: fileName)){
print("Success! Image written in disk")
} else {
print("could not write image")
}
} else {
// Fallback on earlier versions
}
I get the following runtime error:
Error>: kvImagePrintDiagnosticsToConsole: vImageConverter: In this version of the converter, most pixel formats must be packed arrays of samples with no empty space, except as appropriate for kCGImageAlphaNoneSkipFirst/Last. Here, the colorspace has 1 channels, with 1 more for alpha, which should mean (1+1 channels/pixel)*16 bits per channel = 32 bits per pixel. You passed 16 in format->bitsPerPixel.
doubling the size of the buffer and changing the Float32 to Float16 did not work either. The write method returns false.
Topic:
UI Frameworks
SubTopic:
UIKit
Tags: