0

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

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
        }

    }
techno
  • 6,100
  • 16
  • 86
  • 192
  • Please don't repeat your own question. Best approach is to _edit_ the question to make the specific issue clearer. — Also, please note that even this new question is not sufficient to allow someone to reproduce, since you are now failing to show how the image was generated. Try to write a simple MCVE https://stackoverflow.com/help/mcve – matt Jun 02 '18 at 19:30
  • @matt Actually I will delete the old question as the core cause of the issue is image saving and not resizing. – techno Jun 02 '18 at 19:32
  • @matt Why do I need the code to show how the image was generated, it can be loaded from disk, does this make a difference? – techno Jun 02 '18 at 19:33
  • OK fair enough. Basically what I'm suggesting is, just show sufficient workflow for one image, from start to finish, to allow someone to reproduce. Recall that I was not able to do that based on the info you gave (because you failed to mention originally that saving to disk was involved). We don't need all the case statements, so this is not minimal, and we don't know what sort of image is needed to start with, so this is not complete and verifiable. – matt Jun 02 '18 at 19:36
  • If you think there is an issue, then capture a desktop screenshot with a machine with a retina screen. Then take it to a computer with a non-retina screen. See what Preview has to say about the same screenshot. – El Tomato Jun 02 '18 at 21:20

0 Answers0