SceneKit UIImage material is black

1

I'm loading images from the photo library via UIImagePickerController and with that image I'm setting the material of a SCNPlane:

let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
plane.firstMaterial?.diffuse.contents = imageView

With some images, this works fine, no problem at all, the image shows up properly. However, with other images, the texture of the plane shows up as entirely black and Xcode prints "Unsupported IOSurface format: 0x00000000". It seems to be only images that were screenshots causing this, although some screenshot images work just fine.

ios
swift
uiimage
scenekit
arkit
asked on Stack Overflow Apr 3, 2019 by tallen11

1 Answer

1

SceneKit can display both PNGs and JPGs, some PNGs just seem to cause issues. Not sure why or whether it's a bug or not. What you can do that's probably better than converting everything to JPGs (so you can retain transparency) is set your material contents to the image's CGImage:

if let cgImage = image.cgImage {
    geometry.firstMaterial?.diffuse.contents = cgImage
}

You can also convert your image to a JPEG via something like this, as SceneKit doesn't seem to have issues with JPGs:

if let jpegData = image.jpegData(compressionQuality: 1.0) {
    image = UIImage(data: jpegData)!
}

You will lose transparency doing it this way however.

answered on Stack Overflow Apr 3, 2019 by tallen11 • edited Apr 9, 2019 by tallen11

User contributions licensed under CC BY-SA 3.0