【问题标题】:How to predict or calculate the file-size of an image (NSImage) when written to disk?写入磁盘时如何预测或计算图像(NSImage)的文件大小?
【发布时间】:2019-06-29 10:25:19
【问题描述】:

问题说明了一切。

如何在 Swift4.2 或至少在 Swift4 中预测 NSImage 或 NSImageView.image 中图像的文件大小?

我在 Apple 文档中找不到任何相关信息。

我应该将其转换为某种(NS)数据表示还是...

..我应该将图像保存到磁盘,使用 FileManager 检查大小,做我的事情,然后重新保存文件吗?

【问题讨论】:

    标签: file nsfilemanager nsimage swift4.2


    【解决方案1】:

    是的,您需要将图像转换为数据,试试这个可能对您有帮助:

        let image = UIImage(named: "example.jpg")
        if let _data = image?.jpegData(compressionQuality: 1.0)
        {
            print("Size: \(_data.count) bytes")
        }
    

    【讨论】:

    • 那是 ios UIImage 而不是 macos NSImage
    • 我对此不太了解,但请尝试以下代码: var rep = image.representations()[0] as? NSImageRep; var imageSize: NSSize = NSMakeSize(rep?.pixelsWide, rep?.pixelsHigh)
    猜你喜欢
    • 2022-12-06
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多