调整大小分辨率图像的大小,在大小设置为500x500像素时生成1000x1000像素大小

时间:2018-06-01 18:02:47

标签: swift image macos swift4 nsimage

我使用以下扩展方法来调整图像大小。当涉及到大分辨率图像时,即使我将输出大小设置为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
        }

    }

1 个答案:

答案 0 :(得分:0)

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

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

但是当你继续将图像数据保存到磁盘时,你就会忘记这是一个双分辨率图像,你只需保存位图 - 即1000x1000。

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