techno
techno

Reputation: 6508

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

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
        }

    }

Upvotes: 0

Views: 98

Answers (0)

Related Questions