gpt4 book ai didi

Swift - OSX - 调整 NSImage 数据大小

转载 作者:行者123 更新时间:2023-11-30 12:03:19 26 4
gpt4 key购买 nike

我正在尝试调整 NSImage 的大小以将其作为 PFFile 进行解析,并且我需要更改其尺寸以减少其数据大小,但它不起作用。调整大小后的图像在 Swift 中调整了宽度和高度,但与原始图像保持相同的尺寸。

图像为 JPG,尺寸为 2560 x 1706,大小为 721 KB。

当我将其传递给数据时,它保持相同的尺寸并变为 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

到目前为止还没有任何效果。

最佳答案

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

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

关于Swift - OSX - 调整 NSImage 数据大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46952702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com