Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vyshak

macrumors newbie
Original poster
Apr 4, 2020
4
0
I'm using the following code to resize an image.But i keep getting a white border at the bottom


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

I have tried using `floor` function to round off the values.But still the same issue.

4XXzO.jpg


Please advice
 
You shouldn't have to resize it. If you draw it from the image's source rect into a dest rect it should be scaled into the dest rect, right?

Like this:
Code:
[ImageName drawInRect: destRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: kOpaque];

where opaque is usually 1.0.
 
You shouldn't have to resize it. If you draw it from the image's source rect into a dest rect it should be scaled into the dest rect, right?

Like this:
Code:
[ImageName drawInRect: destRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: kOpaque];

where opaque is usually 1.0.
I tried your code.. But the issue still exists.Could you please post an example in SWIFT4.
 
In your project in XCode: go to the help menu and select Developer Documentation. In the control at the upper left of the window select Swift. In the search field enter NSImage. Scroll down in the right pane until you see the the group of methods "Drawing the Image". There you will see six methods for drawing the image. The simplest is func draw(in: NSrect). This draws the image in the specified rectangle. This should be all you need.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.