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

CGImage has no properties / metadata (CGImageProperties)

假设我有一个从 URL 加载的 CGImage,我想通过 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)

输出:

Couldn't get the properties.


但是如果我使用图像所在的 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;
    };
}


Is there a way to retrieve the metadata from a CGImage itself, without having to rely on its source URL?

If not, is there a way to find out the source URL of a given CGImage?

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

CGImage 应该是完全原始的位图数据。最小的未压缩数据集,甚至可以直观地呈现图像。来自 docs "A bitmap image or image mask".

我真的很惊讶你能够以两种不同的方式使用 CGImageSourceCreateWithData 构建源:

  • 在第一种情况下,您直接从未压缩的原始数据创建它,即您的 CGImage。预计它绝对没有 headers 或有关如何显示的其他信息。

  • 在第二种情况下,您是从 JPEG 数据创建它的,该数据是压缩数据 headers,可能包含很多不同的信息。有些信息可能完全不相关,例如图像拍摄地点的坐标或拍摄日期。

因此,您在第二种情况下显示的附加信息可能会被系统用于构建 CGImage object(例如编码)。但它不是为了显示而需要附加到 CGImage 的信息,因为它已经准备好(解码)以供显示。

在您的标题中您说的是 "CGImage has no properties / metadata (CGImageProperties)"。没有"CGImageProperties",有"CGImageSourceProperties"。所以它是具有属性的来源。

所以我相信这些属性不会被复制,并且无法单独从 CGImage 获取它们。不过,您还可以直接从 CGImage 获得其他属性:

  • cgImage.width
  • cgImage.height
  • cgImage.alphaInfo
  • cgImage.bitmapInfo
  • cgImage.bitsPerComponent
  • cgImage.colorSpace
  • ...

你可以多检查一下here