以下代码将NSImage
转换为JPEG数据,但转换后图像将丢失其原始宽度和高度。如何执行转换,同时保持图像的原始尺寸。
let resizedImage = resizeImage(imageData: imageData, maxSize: maxSize) // return type is NSImage
let cgImage = resizedImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!
let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])