如何调整大小 - 重新采样 - 更改文件大小 - NSImage

时间:2017-01-16 13:43:45

标签: swift macos swift3 jpeg nsimage

我在google上做了很多搜索,跟着很多GitHub代码,没有成功。

我需要读取一个jpeg文件,例如DPI = 72,大小= 16000x1096 然后,我需要调整它的大小,以便保存一个72 DPI,800x548像素的jpeg文件 这个,在OSX下,所以我可以使用NSIMAGE,而不是UIImage!

如果我失去一些品质,我不在乎!

最后,在做了som ma-ny尝试后,我把这里的最新代码放在这里,并请求一些帮助:

    // get source image, jpeg, 1600x1196, 72 dpi
    let sourceImage = NSImage(contentsOfFile: source)

    // compute the scale, to have the longest size = 800 pixels. 72 DPI
    let width = CGFloat(sourceImage!.size.width)
    let height = CGFloat(sourceImage!.size.height)
    var scale = CGFloat( 800.0 / width)
    if width < height {
        scale = 800.0 / height
    }
    let newSize:NSSize = NSSize(width: Int(width * scale), height: Int(height * scale))
    print ("new size : " + String(describing: newSize) )

    // create a new image, with the "small" size
    let newImage = NSImage(size: NSSize(width: newSize.width, height: newSize.height))
    // lockfocus : draw will be done in this image
    newImage.lockFocus()
    // interpolation = low, because I don't care of quality
    NSGraphicsContext.current()?.imageInterpolation = NSImageInterpolation.low
    // draw the big image, into the small one
    sourceImage!.draw(in: NSMakeRect(0, 0, newSize.width, newSize.height), from: NSMakeRect(0, 0, width, height), operation: NSCompositingOperation.copy, fraction: CGFloat(1.0))
    newImage.unlockFocus()

    print("newImage size: " + String(describing: newImage.size))
    // display : newImage size: 800 x 598

    let cgRef = newImage.cgImage(forProposedRect: nil, context: nil, hints: nil)
    let newRep = NSBitmapImageRep(cgImage: cgRef!)
    newRep.size = newSize

    let jfifProperties = NSDictionary(dictionary: [kCGImagePropertyJFIFIsProgressive:kCFBooleanTrue])
    let properties = NSDictionary(dictionary: [kCGImageDestinationLossyCompressionQuality:1.0,
                                               kCGImagePropertyJFIFDictionary:jfifProperties,
                                               kCGImagePropertyDPIHeight: 72.0,
                                               kCGImagePropertyDPIWidth:72.0,
                                               kCGImagePropertyPixelHeight: 1.0,
                                               kCGImagePropertyPixelWidth: 1.0
                                               ])

    let data = newRep.representation(using: .JPEG, properties: properties as! [String : Any]) //[:])
    do {
        let url = URL(string: "file://" + destination)
        try data?.write(to: url!) //, options: false)
    }
    catch let error as NSError {
        resizeError = error.localizedDescription
        return false
    }

此代码无法正常运行。我获得了一个144 DPI和1600 x 1096的文件!

如果我在调试器下打印变量newImage,它会显示:

打印newImage的说明: \ n \ T&LT; (kCGColorSpaceICCBased; kCGColorSpaceModelRGB;彩色LCD)&gt; \ n \ t \ twidth = 1600,height = 1196,bpc = 8,bpp = 32,row bytes = 6400 \ n \ t \ tkCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little \ n \ t \ t?是掩码?不,有面具吗?不,有遮面吗?不,应该插入?是&GT;&#34;

我们可以看到,内容似乎与原始内容大小相同,不是吗?

我真的需要一些帮助,拜托,我被封锁了,找不到任何解决办法。

感谢任何答复,谁能帮助我。

祝你好运 奥利弗

1 个答案:

答案 0 :(得分:1)

我自己找到了解决方案,但我很欣赏人们试图帮助我。 谢谢你。

以下是我修复它的方法:

    // get source image, jpeg, 1600x1196, 72 dpi
    let sourceImage = NSImage(contentsOfFile: source)

    // compute the scale, to have the longest size = 800 pixels. 72 DPI
    let width = CGFloat(sourceImage!.size.width)
    let height = CGFloat(sourceImage!.size.height)
    var scale = CGFloat( resizedLongestSize / width)
    if width < height {
        scale = resizedLongestSize / height
    }
    let newSize:NSSize = NSSize(width: Int(width * scale), height: Int(height * scale))

    // create NSBitmapRep manually, if using cgImage, the resulting size is wrong
    let rep = NSBitmapImageRep(bitmapDataPlanes: nil,
                               pixelsWide: Int(newSize.width),
                               pixelsHigh: Int(newSize.height),
                               bitsPerSample: 8,
                               samplesPerPixel: 4,
                               hasAlpha: true,
                               isPlanar: false,
                               colorSpaceName: NSDeviceRGBColorSpace,
                               bytesPerRow: Int(newSize.width * 4),
                               bitsPerPixel: 32)

    let ctx = NSGraphicsContext(bitmapImageRep: rep!)
    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.setCurrent( ctx )
    sourceImage!.draw(in: NSMakeRect(0, 0, newSize.width, newSize.height))
    ctx?.flushGraphics()
    NSGraphicsContext.restoreGraphicsState()

    // Get NSData, and save it
    let data = rep?.representation(using: .JPEG, properties: [:]) // properties as! [String : Any]) //
    do {
        let url = URL(string: "file://" + destination)
        try data?.write(to: url!) //, options: false)
    }
    catch let error as NSError {
        resizeError = error.localizedDescription
        return false
    }

此代码效果很好,并提供一个文件中的图片,已调整大小,并使用一组有限的EXIF数据,如果需要,这些数据很容易填充。

感谢 奥利弗