gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:35:48 25 4
gpt4 key购买 nike

假设我有一个 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)

输出:

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 “位图图像或图像蒙版”。

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

  • 在第一种情况下,您直接从原始未压缩数据创建它,即 CGImage .预计它绝对没有标题或关于如何显示的附加信息。
  • 在第二种情况下,您从 JPEG 数据创建它,该数据是带有可能包含许多不同信息的 header 的压缩数据。某些信息可能完全不相关,例如拍摄图像的坐标或拍摄日期。

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

    在您的标题中,您说“CGImage 没有属性/元数据(CGImageProperties)”。没有“CGImageProperties”之类的东西,只有“CGImageSourceProperties”。所以它是具有属性的源。

    所以我相信这些属性不会被复制,也没有办法从 CGImage 获取它们。独自的。您可以直接从 CGImage 获得其他属性尽管:
  • cgImage.width
  • cgImage.height
  • cgImage.alphaInfo
  • cgImage.bitmapInfo
  • cgImage.bitsPerComponent
  • cgImage.colorSpace
  • ...

  • 您可以查看更多 here .

    关于ios - CGImage 没有属性/元数据 (CGImageProperties),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59612711/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com