调整大小分辨率图像的大小,在大小设置为500x500像素时生成1000x1000像素大小 [英] Resizing Large Resolution Images Producing 1000x1000 Pixels Size when Size is set to 500x500 Pixels

查看:2488
本文介绍了调整大小分辨率图像的大小,在大小设置为500x500像素时生成1000x1000像素大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下扩展方法来调整图像大小。当涉及到大分辨率图像时,即使我将输出大小设置为500x500像素,输出大小仍然是1000x1000像素

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

更新:

//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.

你有一个Retina屏幕。当您调用 lockFocus 并绘制到生成的图形上下文时,图形上下文具有双重分辨率。因此,500x500的大小导致底层位图为1000x1000。这对于显示的目的无关紧要;如果它确实重要,这是一件好事。

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.

但是当你继续将图像数据保存到磁盘时,你会抛弃这是双重的知识 - 分辨率图像,您只需保存位图 - 即1000x1000。

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.

您应该查看Apple有关如何处理高分辨率图像的文档,例如 https://developer.apple.com/ library / content / documentation / GraphicsAnimation / Conceptual / HighResolutionOSX / APIs / APIs.html

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

这篇关于调整大小分辨率图像的大小,在大小设置为500x500像素时生成1000x1000像素大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆