Swift - OSX - 调整NSImage数据的大小

时间:2017-10-26 11:07:07

标签: swift macos resize nsimage nsbitmapimagerep

我正在尝试调整NSImage的大小以将其与解析作为PFFile一起使用,我需要更改其维度以减少其数据大小但它无法正常工作。调整大小的图像调整Swift内部的宽度和高度,但它与原始图像保持相同的尺寸。

图像为JPG,2560 x 1706和721 KB。

当我将它传递给数据时,它保持相同的尺寸和gos到5.7 MB

这是我的代码:

let fotoNSImage = NSImage(byReferencing: url)

let fotoNSImageRedim = redimensionaNSImage(imagem: fotoNSImage, tamanho: NSSize(width: 200, height: 200))

let fotoNSImageRep = NSBitmapImageRep(data: (fotoNSImageRedim.tiffRepresentation)!)

let fotoNSImagePng = fotoNSImageRep!.representation(using: NSBitmapImageRep.FileType.png, properties: [:])

let fotoProduto = FotoProduto(foto: PFFile(data: fotoNSImagePng!)!, categorias: [])

调整图像大小的方法:

static func redimensionaNSImage(imagem: NSImage, tamanho: NSSize) -> NSImage {

    var ratio: Float = 0.0
    let imageWidth = Float(imagem.size.width)
    let imageHeight = Float(imagem.size.height)
    let maxWidth = Float(tamanho.width)
    let maxHeight = Float(tamanho.height)

    if (imageWidth > imageHeight) {
        // Landscape
        ratio = maxWidth / imageWidth;
    }
    else {
        // Portrait
        ratio = maxHeight / imageHeight;
    }

    // Calculate new size based on the ratio
    let newWidth = imageWidth * ratio
    let newHeight = imageHeight * ratio

    // Create a new NSSize object with the newly calculated size
    let newSize: NSSize = NSSize(width: Int(newWidth), height: Int(newHeight))

    // Cast the NSImage to a CGImage
    var imageRect: CGRect = CGRect(x: 0, y: 0, width: Int(newWidth), height: Int(newHeight))
    let imageRef = imagem.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)

    // Create NSImage from the CGImage using the new size
    let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize)

    // Return the new image
    return imageWithNewSize
}

我已经尝试了以下方法但没有成功。

1 - 直接在fotoNSImagePng上更改pixelWide和pixelHigh:

fotoNSImageRep?.pixelsWide = 200
fotoNSImageRep?.pixelsHigh = 133

2 - 创建一个新的NSBitmapImageRep并用它替换图像表示:

let rep = NSBitmapImageRep(bitmapDataPlanes: nil,
                           pixelsWide: 200,
                           pixelsHigh: 133,
                           bitsPerSample: 8,
                           samplesPerPixel: 4,
                           hasAlpha: true,
                           isPlanar: false,
                           colorSpaceName: NSDeviceRGBColorSpace,
                           bytesPerRow: Int(newSize.width * 4),
                           bitsPerPixel: 32)

3 - 遵循这种方法: How to resize - resample - change file size - NSImage

4 - 更改NSBitmapImageRep大小值:

fotoNSImageRep?.size.width = 200
fotoNSImageRep?.size.height = 133

到目前为止没有任何工作。

1 个答案:

答案 0 :(得分:0)

我修改了调整图像大小的方法

static func redimensionaNSImage(imagem: NSImage, tamanho: NSSize) -> NSImage {

    var ratio: Float = 0.0
    let imageWidth = Float(imagem.size.width)
    let imageHeight = Float(imagem.size.height)
    let maxWidth = Float(tamanho.width)
    let maxHeight = Float(tamanho.height)

    // Get ratio (landscape or portrait)
    if (imageWidth > imageHeight) {
        // Landscape
        ratio = maxWidth / imageWidth;
    }
    else {
        // Portrait
        ratio = maxHeight / imageHeight;
    }

    // Calculate new size based on the ratio
    let newWidth = imageWidth * ratio
    let newHeight = imageHeight * ratio

    let imageSo = CGImageSourceCreateWithData(imagem.tiffRepresentation! as CFData, nil)
    let options: [NSString: NSObject] = [
        kCGImageSourceThumbnailMaxPixelSize: max(imageWidth, imageHeight) * ratio as NSObject,
        kCGImageSourceCreateThumbnailFromImageAlways: true as NSObject
    ]
    let size1 = NSSize(width: Int(newWidth), height: Int(newHeight))
    let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSo!, 0, options as CFDictionary).flatMap {
        NSImage(cgImage: $0, size: size1)
    }

    return scaledImage!
}