MacOS - 如何以编程方式计算 Swift 中图像的字节数?
MacOS - How to programmatically count bytes for an image in Swift?
在我的 iOS 项目中,为了计算名为“toto”的 png 图像的字节数,我在控制台中使用了下面这一行:
print(UIImage(imageLiteralResourceName: "toto").pngData()!.count)
但是对于我的 MacOS 项目,无法使用 UIImage,那么我可以使用什么等效项来实现相同的结果?
谢谢。
UIImage 的 MacOS 等价物是 NSImage
编辑:
Here 是类似的东西。
似乎你需要得到一个 NSBitmapImageRep
然后使用 NSBitmapImageRep.representation(using: .png, properties: [:])
您可能已经注意到 NSImage
没有 pngData
方法。您需要先获取图像 tiffRepresentation
,初始化一个新的 NSBitmapImageRep
对象并获取 png
storageType 表示:
let data = NSBitmapImageRep(data: NSImage(imageLiteralResourceName: "toto").tiffRepresentation!)!.representation(using: .png, properties: [:])!
print(data.count)
在我的 iOS 项目中,为了计算名为“toto”的 png 图像的字节数,我在控制台中使用了下面这一行:
print(UIImage(imageLiteralResourceName: "toto").pngData()!.count)
但是对于我的 MacOS 项目,无法使用 UIImage,那么我可以使用什么等效项来实现相同的结果?
谢谢。
UIImage 的 MacOS 等价物是 NSImage
编辑:
Here 是类似的东西。
似乎你需要得到一个 NSBitmapImageRep
然后使用 NSBitmapImageRep.representation(using: .png, properties: [:])
您可能已经注意到 NSImage
没有 pngData
方法。您需要先获取图像 tiffRepresentation
,初始化一个新的 NSBitmapImageRep
对象并获取 png
storageType 表示:
let data = NSBitmapImageRep(data: NSImage(imageLiteralResourceName: "toto").tiffRepresentation!)!.representation(using: .png, properties: [:])!
print(data.count)