0

I'm using the following code to Resize an NSimage

func resizeinbuilt(withSize targetSize: NSSize) -> NSImage? {
        let frame = NSRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
        guard let representation = self.bestRepresentation(for: frame, context: nil, hints: nil) else {
            return nil
        }
        let image = NSImage(size: targetSize, flipped: false, drawingHandler: { (_) -> Bool in
            return representation.draw(in: frame)
        })

        return image
    }

Strangely the saved image size in pixels is 400x400 even though on debugging I can clearly see the size as the 100x100

enter image description here

This is the save to disk code

 func saveimage(face:NSImage,imageURL:String,format:String) -> Bool
    {
       
        let cgRef = face.cgImage(forProposedRect: nil, context: nil, hints: nil)
        var newRep: NSBitmapImageRep? = nil
        if let aRef = cgRef {
            newRep = NSBitmapImageRep(cgImage: aRef)
        }
        newRep?.size = face.size ?? NSSize.zero
        
        switch format {
        case ".png":
            let filepath=URL(fileURLWithPath: imageURL+".png")
            let dataToSave = newRep?.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 = newRep?.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 = newRep?.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 = newRep?.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 = newRep?.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
  • Does this answer your question? [How to resize NSImage?](https://stackoverflow.com/questions/11949250/how-to-resize-nsimage) – Willeke Apr 23 '22 at 10:12
  • @Willeke This does not work, same issue. – techno Apr 23 '22 at 11:49
  • How do you save the image? Post a [mre] please. – Willeke Apr 23 '22 at 13:09
  • Your resizing function doesn't actually do any resizing at all. All you've done is express to the computer "Out of all the image representations for this image (they could be multiple), pick the one that's most appropriate for rendering at 400x400 with no further context or hints". What you get back is one of the existing image reps, with no actual resizing going on. – Alexander Apr 23 '22 at 15:15
  • @Alexander Can you please guide me to a proper resizing example? – techno Apr 23 '22 at 17:50
  • 1
    @techno Depends on your requirements. What are you trying to resize the image for? Where is going to be used? – Alexander Apr 23 '22 at 17:50
  • @Alexander The image is going to be saved to disk.Please see the update. – techno Apr 24 '22 at 03:50
  • See [Saving NSImage in Different Formats Locally](https://stackoverflow.com/questions/46432709/saving-nsimage-in-different-formats-locally). – Willeke Apr 24 '22 at 09:27
  • @Willeke Does not fix the issue. – techno Apr 24 '22 at 17:46
  • @Willeke actually it might have fixed the issue. Will test further. – techno Apr 24 '22 at 18:04

1 Answers1

-2

Try this:

Edited

func resize(_ image: NSImage, w: Int, h: Int) -> NSImage {
    var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
    var newImage = NSImage(size: destSize)
    newImage.lockFocus()
    image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.CompositeSourceOver, fraction: CGFloat(1))
    newImage.unlockFocus()
    newImage.size = destSize
    return newImage!
}

Usage:

self.resize(NSImage(named: "yourImageName")!, w: 200, h: 200)
Amin Rezaew
  • 298
  • 1
  • 14