0

I'm trying to convert the data from a .obj file to a base64 string, so that I can display a thumbnail preview of the .obj file in an NSImageView.

Below is my code for getting the asset from a local url, and converting it to a base64 string. I then convert that to Data, which I then attempt to convert to an image.

    let fileUrl = Foundation.URL(string: input_string)


    let data: NSData? = NSData(contentsOfFile: input_string)

    if let fileData = data {

        let base64String = fileData.base64EncodedString()

        let decodedData = NSData(base64Encoded: base64String, options: NSData.Base64DecodingOptions(rawValue: 0) )

        let decodedimage = NSImage(data: decodedData! as Data)
}

I can see that I'm getting the data set, but decodedImage is always nil - I'm guessing this has something to do with .obj image format, which I may not know enough about. Any insight / help appreciated.

EDIT: Here's a screenshot showing what the .obj file looks like in the finder preview.

enter image description here

narner
  • 2,908
  • 3
  • 26
  • 63
  • @MartinR In this case it's just a simple blob - I updated my question with a screenshot of a finder preview of it. – narner Nov 28 '17 at 12:48
  • Not related to your question but if input_string is a file path you should use `URL(fileURLWithPath:)` initializer. The URL(string:) initializer requires that the string contains the url scheme, which in this case of a fileURL path would be "file://". Besides that you should update your Xcode/Swift to the latest version (Swift 2 is not even supported anymore by Xcode9). Btw looks like you have your own struct or class named URL you should change its name to something else – Leo Dabus Nov 28 '17 at 13:18

1 Answers1

1

OBJ is not an image format. Its an old 3D model format. If you want to convert it to an image you will need to open the file and add it to a SCNScene as a geometry node and then render out the the SCNView to an image and save that image.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • That does make sense, Josh. But, how then would the finder be able to render a preview as in the screenshot I posted? – narner Nov 28 '17 at 12:59
  • I assume finder does exactly the same thing; except that it probably caches the resulting thumbnail so it doesn't have to rerender it every time. Some 3d formats to include a thumbnail; obj isn't one of them. – Josh Homann Nov 28 '17 at 13:01