Im using the following extension method to resize an image.When it comes to large resolution images the output size remains 1000x1000 pixels even when I set the output size to 500x500 pixels
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
}
What im I doing wrong? Please advice
UPDATE:
//SAVING
for x in fileArray {
var image = NSImage(contentsOf:x)!
let imageURL=outdir+"/"+"xxx"
image=image.resizeImage(width: CGFloat(rwidth), CGFloat(rheight))
saveimage(xdata: image, imageURL: imageURL, format: fileformat)
}
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
}
}
The problem is that you are not taking into account the scale of the image you're creating.
You have a Retina screen. When you call lockFocus
and draw into the resulting graphics context, the graphics context has double-resolution. Thus, a size of 500x500 results in an underlying bitmap of 1000x1000. That doesn't matter for purposes of display; to the extent that it does matter, it's a good thing.
But when you proceed to save the image data to disk, you throw away the knowledge that this is a double-resolution image, and you simply save the bitmap — which is 1000x1000.
You should look at Apple's documentation on how to deal with high-resolution images, e.g. https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/APIs/APIs.html
来源:https://stackoverflow.com/questions/50648837/resizing-large-resolution-images-producing-1000x1000-pixels-size-when-size-is-se