情况: 用于上传;我需要客户端检查图像是否是来自 Theta S 或 Gear 360 等相机的 360° 图像。 通过检查 XMP 元数据,这应该相当容易完成。
但是,ImageIO 似乎忽略了 XMP 元数据,如果运行下面的示例,ImageIO 返回的数据中没有 XMP 条目。
斯威夫特3:
import ImageIO
let url = URL(fileURLWithPath: filePath)
let imageData:Data = try! Data(contentsOf: url)
if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable:Any]
{
print(imageProperties) // no xmp metadata?
}
有什么方法可以从图像 NSData 中减去 XMP-xml 吗?
我发现您可以通过
CGImageSourceCopyMetadataAtIndex
访问此信息。
粗略代码示例:
斯威夫特3:
if let imageSource = CGImageSourceCreateWithData(nsData as CFData, nil),
let imageProperties = CGImageSourceCopyMetadataAtIndex(imageSource, 0, nil) {
var result:String = ""
CGImageMetadataEnumerateTagsUsingBlock(imageProperties, nil, nil, { (key, tag) -> Bool in
let tagString:NSString = CGImageMetadataTagCopyName(tag) as! NSString
if tagString == "ProjectionType" {
result = CGImageMetadataTagCopyValue(tag) as! NSString
return false
}
return true
})
print(result) //equirectangular
}
如果只是检查 XMP 元数据中的一个特定值,您也可以像这样搜索它,而不是枚举:
guard let source = CGImageSourceCreateWithData(self as NSData, nil) else { return }
guard let metadata = CGImageSourceCopyMetadataAtIndex(source, 0, nil) else { return }
let tag = CGImageMetadataCopyTagWithPath(metadata, nil, "GPano:ProjectionType" as NSString)