首页 > 解决方案 > 当大小设置为 500x500 像素时,调整生成 1000x1000 像素大小的高分辨率图像的大小

问题描述

我使用以下扩展方法来调整图像大小。对于大分辨率图像,即使我将输出大小设置为 500x500 像素,输出大小仍保持为 1000x1000 像素

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
    }

我做错了什么?请指教

更新:

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

    }

标签: swiftimagemacosswift4nsimage

解决方案


问题是您没有考虑您正在创建的图像的比例。

你有一个视网膜屏幕。当您调用lockFocus并绘制到生成的图形上下文中时,图形上下文具有双分辨率。因此,500x500 的大小会产生 1000x1000 的底层位图。出于显示目的,这无关紧要。就它确实重要的程度而言,这是一件好事。

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

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


推荐阅读