How to store an NSImage as Data in an app group on macOS

436 views Asked by At

In iOS I can store UIImage to an app group as Data. I can convert the png image with pngData() to a Data object:

let imageData = scaledImage.pngData()!

I store that object in an app group, retrieve it and convert ik back to an UIImage:

let image = UIImage(data: imageData)

It's works great, but it doesn't work on macOS. MacOS doesn't have a UIImage, but a NSImage. How can I convert an NSImage tot Data and back?

Update: I use this code for macOS

let image = NSImage(named: "axl")! 
let imageData = image.tiffRepresentation!

I store the imageData in a array.

In another part of the code I get the imageData from the array and convert it back:

let imageData = entry.images[0]                     
let image = NSImage(data: imageData)
                                    

Somehow the tiffRepresentation converting back with NSImage(data: ...) doesn't work.

Update: it does work!!

1

There are 1 answers

0
Patrick Koning On BEST ANSWER

I use this code for macOS

let image = NSImage(named: "axl")! 
let imageData = image.tiffRepresentation!

I store the imageData in a array.

In another part of the code I get the imageData from the array and convert it back:

let imageData = entry.images[0]                     
let image = NSImage(data: imageData)                           

It does work!!