In swift how to determine the final file-size from an image in NSImage or NSimageView

Hi,

Using Swift 4.2 and 5 ( jups already busy )

How can I determine the file-size of an image what I have loaded, or created, and manipulated with drawings inside an NSImage and NSImageView?

I can save the file each time to check the size, but sound silly.


Should I move that as data in (NS)Data?

Then when I know the size, is there any overhead bytes when I save the Image to disk?

Or to CloudKit?



With kind regards

Replies

The file size will depend in which format you save the image.

You don't need to save again, just read the attributes of the file


        if (fileManager.fileExists(atPath: fileName)) {
            if let image = UIImage(contentsOfFile: fileName) {
                do {
                attr = try fileManager.attributesOfItem(atPath: fileName) as NSDictionary 
                if let _attr = attr {
                    fileSize = _attr.fileSize() / 1024; // in KB
                    }
                }
                catch {       
                 print("Error fileName")
                }
            }
        }

You can determine the raw byte size of the image in memory:


Read details here

https://stackoverflow.com/questions/34262791/how-to-get-image-file-size-in-swift

Thank you for your reply.

Isn't this code when the file is saved? I did wrote I can save the file, but actualy I don't want that.

I want to know the size before it is saved. When it is in the NSImageView. F.e I choose a representaion type, like PNG, and then I want to calculate the final size. I see samples about ios but none for macos.

Should I move that as data in (NS)Data?

Very likely. It's very hard to predict the data size of a compressed image, and I cannot find any APIs to get only the data size. Actually compressing seems to be the right and the only way.


Then when I know the size, is there any overhead bytes when I save the Image to disk?

Or to CloudKit?

I may be mistaking what you mean, but when you store a data in a file system, the file size becomes exactly the same as the data.

If you mean some extra disk space other than the image file itself, such as directory entry or i-node or something like that, it's hard to know how much would be needed.

Thank you,


My overhead concern is, if the NSImage view image data part, already got/contains representation info about what kind of image type like 'PNG', 'GIF' e.t.c. it is. Or if that happens, adding this and compression ( if saved ) info, during saving time?


Today I will start reading, again, NSIMageRep and the rest of the family and try to get that better in my head.


Just pity that a lot of the documentation is at a minimum ( paid by the letter ?) or the samples are outdated or just Obj-C.

Wish they keep the samples, more up to date, and the in different versions, like Obj-C, Swift4-4.2 and now of course Swift 5.

For them it isn't to much work, but it saves a lot of misunderstanding and unwanted searching by the developers. ( did send this to radar )

My overhead concern is, if the NSImage view image data part, already got/contains representation info about what kind of image type like 'PNG', 'GIF' e.t.c. it is. Or if that happens, adding this and compression ( if saved ) info, during saving time?


Then you have no need to worry. The generated PNG data or GIF data, already contains such info inside the data. As I already noted, no extra space when saving data.


Today I will start reading, again, NSIMageRep and the rest of the family and try to get that better in my head.


Mac OS progamming has a long history, and many still-useful documents and samples are marked as archived and even now, no direct replacements for them are provided... I hope Apple would listen to our voices.


Anyway, if you find some difficulty on using NSImageRep or something, there are many readers who can help you in the forums.