Question about image management


Hello everyone,

I have a question about documentation for image management.

In the documentation we find this example :

// Define your models like regular Swift classes
class Dog: Object {
@objc dynamic var name = “”
@objc dynamic var age = 0
class Person: Object {
@objc dynamic var name = “”
@objc dynamic var picture: Data? = nil // optionals supported

Imagine that I put a PNG file named « pictureA » in the asset folder. And if I create a Person object « personA ».

How do I write the code to assign the picture to the Person object ?

Previously, I will simply have written :

In class Person :
var picture = UIImage()

After :
let personA = Person()
personA.picture = UIImage(named: « pictureA »)

Thanks for your help.


Your code seems fine for the first glance, however, if you are not syncing the images, then you are better to have pictureName: String? model.

On the second glance, one user probably can have more than one image, so you need to store a List of image names (RealmList). If you want to store a new image, then simply use person.images.add(image)


Thank you for your reply.

I think I understood.

But most importantly, I would like to understand how to use « data » as in the example.


I wouldn’t recommend using data in any DB - but if you can’t avoid it…

You can convert an image to data in swift like
let imageData: NSData = UIImagePNGRepresentation(myImage)
then you can save it to a person as a person.picture = imageData.

On read, you can convert the data back to an image.
Don’t forget, that any changes on a managed realm object need to be in a write transaction :slight_smile:


Thanks for your advices.

So if I understood correctly I have to prepare my class like this :

@objc dynamic var pictureName : String = « »
var picture = UIImage()

Which makes it possible not to store the photo in the DB.


You can store the image as Data as you write, but it’s not recommended.



I used the image name to display an image in my UICollectionView. Like this :

imageView.image = UIImage (named: itemMenu.nomImage)

For precision, “nomImage” = String.
It works, the image is displayed, but I get this warning:

2019-02-10 12: 43: 30.413603 + 0100 JuraTrip [12545: 829252] [framework] CUICatalog: Invalid asset name supplied: '' "

I have to use the name of the image differently?


you get this warning when you try to use UIImage(named: nil).


My .02:

Whether or not to store objects as Data (NSData) is really use-case dependent. For this use case, assuming it’s a reasonable size image that’s a jpg or png that will never have any other operations performed on it, storing is as a Data type is probably fine.

A quick macOS example

A class to hold our favorite starship pic

class ShipClass: Object {
    @objc dynamic var ship_name = ""
    @objc dynamic var ship_image_data: Data?

and then code to write it

    if let image = NSImage(named:"Enterprise.png") {
        let imageData = image.tiffRepresentation
        let s0 = ShipClass()
        s0.ship_name = "Enterprise"
        s0.ship_image_data = imageData
        if let realm = gGetRealm() {
            try! realm.write {

and then code to read it back in and display it in a NSImage called shipImage

   if let realm = gGetRealm() {
        let shipResults = realm.objects(ShipClass.self)
        if shipResults.count > 0 {
            let firstShip = shipResults.first!
            let name = firstShip.ship_name
            let shipData = firstShip.ship_image_data!

            let decodedImage = NSImage(data: shipData)
            self.shipImage.image = decodedImage

*I have a singleton to return my realm called with gGetRealm() and there is no error checking here so if you use this, please add some.



It was precisely when the images are displayed that I get this message.
I restarted Xcode and it looks like it’s back to normal.


Thank you very much for this explanation.
I understand the mechanism.