swiftxcodeios-app-groupappgroups

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


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!!


Solution

  • 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!!