CGImage没有属性/元数据(CGImageProperties)

问题描述 投票:1回答:1

说我有一个从CGImage加载的URL,我想通过CGImageSourceCopyPropertiesAtIndex提取其属性:

// Playground

import SwiftUI

func printPropertiesOf(_ image: CGImage) {
    guard let dataProvider = image.dataProvider else {
        print("Couldn't get the data provider.")
        return
    }
    guard let data = dataProvider.data else {
        print("Couldn't get the data.")
        return
    }
    guard let source = CGImageSourceCreateWithData(data, nil) else {
        print("Couldn't get the source.")
        return
    }
    guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) else {
        print("Couldn't get the properties.")
        return
    }
    print(properties)
}

let url = Bundle.main.url(forResource: "Landscape/Landscape_0", withExtension: "jpg")!
let source = CGImageSourceCreateWithURL(url as CFURL, nil)!
let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil)!

printPropertiesOf(cgImage)

输出:

无法获取属性。


但是如果我使用图像所在的URL,而不是CGImage

// Playground

import SwiftUI

func printPropertiesOfImageIn(_ url: URL) {
    guard let data = try? Data(contentsOf: url) else {
        print("Couldn't get the data.")
        return
    }
    guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
        print("Couldn't get the source.")
        return
    }
    guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) else {
        print("Couldn't get the properties.")
        return
    }
    print(properties)
}

let url = Bundle.main.url(forResource: "Landscape/Landscape_0", withExtension: "jpg")!
let source = CGImageSourceCreateWithURL(url as CFURL, nil)!
let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil)!

printPropertiesOfImageIn(url)

输出:

{
    ColorModel = RGB;
    DPIHeight = 72;
    DPIWidth = 72;
    Depth = 8;
    PixelHeight = 1200;
    PixelWidth = 1800;
    "{JFIF}" =     {
        DensityUnit = 1;
        JFIFVersion =         (
            1,
            0,
            1
        );
        XDensity = 72;
        YDensity = 72;
    };
    "{TIFF}" =     {
        Orientation = 0;
        ResolutionUnit = 2;
        XResolution = 72;
        YResolution = 72;
    };
}


是否有一种方法可以从CGImage本身检索元数据,不必依赖其源URL?

如果没有,是否有办法找出给定的来源URLCGImage

((注:以上示例can be found here中使用的图像。)

ios swift macos core-graphics cgimage
1个回答
0
投票

CGImage应该完全是原始位图数据。最少一组未压缩的数据,即使在视觉上也可以呈现图像。从docs“位图图像或图像蒙版”。

© www.soinside.com 2019 - 2024. All rights reserved.