Resize NSImage doesn't reduce bytes ?

I've tried several versions of this but resizing the image doesn't reduce the bytes ...


            var image = NSImage(data: data)!

            let new_width: CGFloat = 200
            let new_height = new_width * aspect
            
            
            let newSize = NSSize(width: new_width, height: new_height)

            if let bitmapRep = NSBitmapImageRep(
                bitmapDataPlanes: nil, pixelsWide: Int(newSize.width), pixelsHigh: Int(newSize.height),
                bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
                colorSpaceName: .calibratedRGB, bytesPerRow: 0, bitsPerPixel: 0
                ) {
                bitmapRep.size = newSize
                NSGraphicsContext.saveGraphicsState()
                NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmapRep)
                image.draw(in: NSRect(x: 0, y: 0, width: newSize.width, height: newSize.height), from: .zero, operation: .copy, fraction: 1.0)
                NSGraphicsContext.restoreGraphicsState()
                
                let resizedImage = NSImage(size: newSize)
                resizedImage.addRepresentation(bitmapRep)
                
                image = resizedImage
            }

Replies

I think you have to use image compression API (for JPEG for instance) to effectively reduce the memory footprint.


Here, it is just changing the display.

a. How do you examine the byte count of the original image? The copied image?


b. How do you ensure that the original image is deallocated?

The Byte Count can come from the Data() or NSData() ".count". I created a custom NSImage Class to check when replacing the "image" with "resizedImage" that it does Deallocate / Deinitialize, will only work with "var" not "let" to make sure it's an optional value.

Yeh, was just creating a new sized image without resampled data, thanks.


Incase anyone wonders here's an update (also tried making data smaller by compression and jpeg not png) ...

        if let rep = NSBitmapImageRep(bitmapDataPlanes: nil,
                                      pixelsWide: Int(new_width),
                                      pixelsHigh: Int(new_height),
                                      bitsPerSample: 8,
                                      samplesPerPixel: 4,
                                      hasAlpha: true,
                                      isPlanar: false,
                                      colorSpaceName: NSColorSpaceName.deviceRGB,
                                      bytesPerRow: 0,
                                      bitsPerPixel: 0)
        {
            let ctx = NSGraphicsContext(bitmapImageRep: rep)
            NSGraphicsContext.saveGraphicsState()
            NSGraphicsContext.current = ctx
            let rect = NSRect(x: 0, y: 0, width: new_size.width, height: new_size.height)
            image.draw(in: rect)
            ctx?.flushGraphics()
            NSGraphicsContext.restoreGraphicsState()
            
            let compression_factor = 0.35
            let options: [NSBitmapImageRep.PropertyKey: Any] = [.compressionFactor: compression_factor]
            
            if let data = rep.representation(using: .jpeg, properties: options) {
                image = NSImage(data: data)!
            }