Saving NSImage Produces 2X Size Image -Issues on Retina Screen

Im using the following code to save NSImage to different formats.But due to the retina screen issue as mentioned in many SO Questions the image that is saved has 2X Resolution.I understand that using

lockfocus()
causes this issue.But im not using
lockfocus()
anywhere in the code. How can I modify this code to make it work irrespective of the screen size/resolution.Please advice


func saveimage(xdata:NSImage,imageURL:String,format:String) -> Bool
    {


        let bMImg = NSBitmapImageRep(data: (xdata.tiffRepresentation)!)
        switch format {
        case ".png":
            let filepath=URL(fileURLWithPath: imageURL+".png")
            let dataToSave = bMImg?.representation(using: NSBitmapImageRep.FileType.png, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
            do
            {
                try  dataToSave?.write(to: filepath)
                return true

            } catch
            {
                return false
            }
        case ".jpg":
            let filepath=URL(fileURLWithPath: imageURL+".jpg")
             let dataToSave = bMImg?.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
            do
            {
                try  dataToSave?.write(to:filepath)
                return true

            } catch
            {
               return false
            }
        case ".tif":
            let filepath=URL(fileURLWithPath: imageURL+".tif")
            let dataToSave = bMImg?.representation(using: NSBitmapImageRep.FileType.tiff, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
            do
            {
                try  dataToSave?.write(to:filepath)
                return true

            } catch
            {
                return false
            }
        case ".bmp":
            let filepath=URL(fileURLWithPath: imageURL+".bmp")
            let dataToSave = bMImg?.representation(using: NSBitmapImageRep.FileType.bmp, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
            do
            {
                try  dataToSave?.write(to:filepath)
                return true

            } catch
            {
                return false
            }
        case ".gif":
             let filepath=URL(fileURLWithPath: imageURL+".gif")
            let dataToSave = bMImg?.representation(using: NSBitmapImageRep.FileType.gif, properties: [NSBitmapImageRep.PropertyKey.compressionFactor : 1])
            do
            {
                try  dataToSave?.write(to:filepath)
                return true

            } catch
            {
                return false
            }

        default:

            return true
        }

    }

Replies

What problem does it create ?

- size of file ?

- the display of the image when you call it later ?

Im using the following extention to resize an image

extension NSImage {
    func resizeImage(width: CGFloat, _ height: CGFloat) -> NSImage {
        let img = NSImage(size: CGSize(width:width, height:height))

        img.lockFocus()
        let ctx = NSGraphicsContext.current
        ctx?.imageInterpolation = .high
        self.draw(in: NSMakeRect(0, 0, width, height), from: NSMakeRect(0, 0, size.width, size.height), operation: .copy, fraction: 1)
        img.unlockFocus()

        return img
    }

When i resize the image to 500x500 Pixels and save it to disk like this

var image = NSImage(contentsOf:x)!
            let imageURL=outdir+"/"+"***"
            image=image.resizeImage(width: CGFloat(rwidth), CGFloat(rheight))
            saveimage(xdata: image, imageURL: imageURL, format: fileformat)


The resulting image is around 1000x100 Pixels almost 2X the specified size,it seems to be occuring due to the Retina Screen resolution issue.How can i fix this problem? Please advice.

Sure it is due to Retina.


On iPhone 6Plus, you would evan have 3x.


Look here:

h ttps://stackoverflow.com/questions/27239668/psd-pixels-to-ios-points

If I'm looking at the same SO thread as you, that code is 5 yrs. old. I'd ignore it and find something more current that includes 3x, as an example.