调整图像大小并保存到文件(resizing image and saving to file)

编程入门 行业动态 更新时间:2024-10-09 09:24:48
调整图像大小并保存到文件(resizing image and saving to file)

我有这些功能,我拼凑在一起调整大小并保存图像。 但它似乎没有正确调整我的图像大小 - 150x150图像试图调整大小,因为50x50图像最终保存为100x100。 是什么原因导致它?

extension NSImage { @discardableResult func saveAsPNG(url: URL) -> Bool { guard let tiffData = self.tiffRepresentation else { print("failed to get tiffRepresentation. url: \(url)") return false } let imageRep = NSBitmapImageRep(data: tiffData) guard let imageData = imageRep?.representation(using: .PNG, properties: [:]) else { print("failed to get PNG representation. url: \(url)") return false } do { try imageData.write(to: url) return true } catch { print("failed to write to disk. url: \(url)") return false } } } enum error:Error { case imageCreationFailure } func resizeImageByFactor(_ url:URL) throws { let image = NSImage(byReferencing: url) guard image.isValid else { throw error.imageCreationFailure } let reSize = NSSize(width: 50, height: 50) let oldRect = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height) let newRect = CGRect(x: 0.0, y: 0.0, width: reSize.width, height: reSize.height) let newImage = NSImage(size: reSize) newImage.lockFocus() image.draw(in: newRect, from: oldRect, operation: .copy, fraction: 1.0) newImage.unlockFocus() newImage.size let url = URL(fileURLWithPath: "test.jpg", relativeTo: url.deletingLastPathComponent()) newImage.saveAsPNG(url: url) }

I have these functions that I've cobbled together to resize and save an image. But it doesn't seem to be resizing my images properly -- a 150x150 image attempted to be resized as 50x50 image ends up saved as 100x100. Any ideas what's causing it?

extension NSImage { @discardableResult func saveAsPNG(url: URL) -> Bool { guard let tiffData = self.tiffRepresentation else { print("failed to get tiffRepresentation. url: \(url)") return false } let imageRep = NSBitmapImageRep(data: tiffData) guard let imageData = imageRep?.representation(using: .PNG, properties: [:]) else { print("failed to get PNG representation. url: \(url)") return false } do { try imageData.write(to: url) return true } catch { print("failed to write to disk. url: \(url)") return false } } } enum error:Error { case imageCreationFailure } func resizeImageByFactor(_ url:URL) throws { let image = NSImage(byReferencing: url) guard image.isValid else { throw error.imageCreationFailure } let reSize = NSSize(width: 50, height: 50) let oldRect = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height) let newRect = CGRect(x: 0.0, y: 0.0, width: reSize.width, height: reSize.height) let newImage = NSImage(size: reSize) newImage.lockFocus() image.draw(in: newRect, from: oldRect, operation: .copy, fraction: 1.0) newImage.unlockFocus() newImage.size let url = URL(fileURLWithPath: "test.jpg", relativeTo: url.deletingLastPathComponent()) newImage.saveAsPNG(url: url) }

最满意答案

OS X和iOS设备具有缩放因子。 iPhone 5,5S,6等都具有2倍的缩放系数。 iPhone 6 Plus的缩放系数为3倍。 旧的非视网膜iPhone具有1倍的缩放因子。 我的带有4K显示器的OS X机器具有2倍的缩放系数。

你应该做的是:

let scalingFactor = NSScreen.mainScreen()?.backingScaleFactor; let size = NSSize(width: 50 / scalingFactor!, height: 50 / scalingFactor!);

OS X & iOS devices have scaling factors. The iPhone 5, 5S, 6, etc. all have a scaling factor of 2x. The iPhone 6 Plus has a scaling factor of 3x. The old non-retina iPhones have a 1x scaling factor. My OS X machine with a 4K display has a scaling factor of 2x.

What you should do is this:

let scalingFactor = NSScreen.mainScreen()?.backingScaleFactor; let size = NSSize(width: 50 / scalingFactor!, height: 50 / scalingFactor!);

更多推荐

本文发布于:2023-08-01 18:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364640.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并保存   图像   大小   文件   saving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!