swift - 当大小设置为 500x500 像素时,调整生成 1000x1000 像素大小的高分辨率图像的大小
问题描述
我使用以下扩展方法来调整图像大小。对于大分辨率图像,即使我将输出大小设置为 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
}
}
解决方案
问题是您没有考虑您正在创建的图像的比例。
你有一个视网膜屏幕。当您调用lockFocus
并绘制到生成的图形上下文中时,图形上下文具有双分辨率。因此,500x500 的大小会产生 1000x1000 的底层位图。出于显示目的,这无关紧要。就它确实重要的程度而言,这是一件好事。
但是当您继续将图像数据保存到磁盘时,您会抛弃这是双分辨率图像的知识,而只需保存位图——即 1000x1000。
您应该查看 Apple 关于如何处理高分辨率图像的文档,例如https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/APIs/APIs.html
推荐阅读
- r - 使用 purrr 按向量中的值过滤
- python - 使用 XGBoost 进行 ML 预测(棘手的时间序列)
- android - 即使有 Flex 父级,Flutter Expanded 小部件也会出错
- r - 终止涉及根函数和事件函数的 ODE 求解器
- python - 如何使用 Python 从 CGI 网页读取数据?
- php - PHP回显变量在while循环之外返回最后一个值
- google-apps-script - 有没有办法使用 Google 表格/Google Apps 脚本将条件格式转换为“静态”格式?
- python - 如何循环遍历多个数据框以根据行条件选择数据框?
- python - 为什么干净的 Midi 文件播放不同?(与米多)
- kubernetes-helm - Helm Charts - 如何在未定义的对象属性值上使用“默认”?