【发布时间】:2017-08-23 01:49:43
【问题描述】:
这是我第一次尝试 Swift 3 和 Xcode 8.3.3。我正在尝试通过命令行应用程序 Exiftool 解析从文件中提取的 JSON 元数据。
let task = Process()
let filePath = url.path
let etPath = "/usr/local/bin/exiftool"
task.launchPath = etPath
task.arguments = ["-a", "-g1", "-json", filePath]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
let mydata = output?.data(using: String.Encoding.utf8.rawValue)!
do {
let myJson = try JSONSerialization.jsonObject(with: mydata!, options: []) as AnyObject
if let XMPdc = myJson["XMP-dc"] as AnyObject? {
if let creator = XMPdc["Creator"] as! NSArray? {
print(creator)
}
}
} catch let error as NSError {
print(error)
}
脚本的第一部分运行良好,允许我将 JSON 数据放入变量 myJson。如果我打印出那个变量,我会得到这个:
(
{
Composite = {
ImageSize = 100x100;
Megapixels = "0.01";
};
ExifIFD = {
ColorSpace = Uncalibrated;
ExifImageHeight = 100;
ExifImageWidth = 100;
};
ExifTool = {
ExifToolVersion = "10.61";
};
File = {
ExifByteOrder = "Little-endian (Intel, II)";
FileType = TIFF;
FileTypeExtension = tif;
MIMEType = "image/tiff";
};
"ICC-header" = {
CMMFlags = "Not Embedded, Independent";
ColorSpaceData = "RGB ";
ConnectionSpaceIlluminant = "0.9642 1 0.82487";
DeviceAttributes = "Reflective, Glossy, Positive, Color";
DeviceManufacturer = KODA;
DeviceModel = ROMM;
PrimaryPlatform = "Microsoft Corporation";
ProfileCMMType = KCMS;
ProfileClass = "Display Device Profile";
ProfileConnectionSpace = "XYZ ";
ProfileCreator = KODA;
ProfileDateTime = "1998:12:01 18:58:21";
ProfileFileSignature = acsp;
ProfileID = 0;
ProfileVersion = "2.1.0";
RenderingIntent = "Media-Relative Colorimetric";
};
"ICC_Profile" = {
BlueMatrixColumn = "0.03134 9e-05 0.82491";
BlueTRC = "(Binary data 14 bytes, use -b option to extract)";
DeviceMfgDesc = KODAK;
DeviceModelDesc = "Reference Output Medium Metric(ROMM) ";
GreenMatrixColumn = "0.13519 0.71188 0";
GreenTRC = "(Binary data 14 bytes, use -b option to extract)";
MakeAndModel = "(Binary data 40 bytes, use -b option to extract)";
MediaWhitePoint = "0.9642 1 0.82489";
ProfileCopyright = "Copyright (c) Eastman Kodak Company, 1999, all rights reserved.";
ProfileDescription = "ProPhoto RGB";
RedMatrixColumn = "0.79767 0.28804 0";
RedTRC = "(Binary data 14 bytes, use -b option to extract)";
};
IFD0 = {
Artist = Autore;
BitsPerSample = "16 16 16";
Compression = LZW;
ImageDescription = "Creator: Test ; Date: 1900";
ImageHeight = 100;
ImageWidth = 100;
ModifyDate = "2017:08:10 10:58:42";
Orientation = "Horizontal (normal)";
PhotometricInterpretation = RGB;
PlanarConfiguration = Chunky;
ResolutionUnit = inches;
RowsPerStrip = 100;
SamplesPerPixel = 3;
Software = "Adobe Photoshop CC 2015 (Macintosh)";
StripByteCounts = 1037;
StripOffsets = 11450;
SubfileType = "Full-resolution Image";
XResolution = 300;
YCbCrPositioning = "Co-sited";
YResolution = 300;
};
Photoshop = {
DisplayedUnitsX = inches;
DisplayedUnitsY = inches;
GlobalAltitude = 30;
GlobalAngle = 90;
HasRealMergedData = Yes;
IPTCDigest = 00000000000000000000000000000000;
PhotoshopThumbnail = "(Binary data 557 bytes, use -b option to extract)";
PixelAspectRatio = 1;
PrintPosition = "0 0";
PrintScale = 1;
PrintStyle = Centered;
ReaderName = "Adobe Photoshop CC 2015";
SlicesGroupName = "";
"URL_List" = (
);
WriterName = "Adobe Photoshop";
XResolution = 300;
YResolution = 300;
};
"XMP-dc" = {
Creator = Autore;
Description = "Creator: Test ; Date: 1900";
Format = "image/tiff";
Publisher = "-";
Rights = "-";
Subject = "-";
Title = tite;
};
"XMP-pdf" = {
Producer = "-";
};
"XMP-photoshop" = {
ColorMode = RGB;
ICCProfileName = "ProPhoto RGB";
};
"XMP-x" = {
XMPToolkit = "Image::ExifTool 10.53";
};
"XMP-xmp" = {
CreateDate = 1900;
CreatorTool = "Adobe Photoshop CC 2015 (Macintosh)";
MetadataDate = "2017:08:10 10:58:42-04:00";
ModifyDate = "2017:08:10 10:58:42-04:00";
ThumbnailFormat = "-";
ThumbnailImage = "(Binary data 48 bytes, use -b option to extract)";
};
"XMP-xmpRights" = {
Marked = 1;
};
}
)
但是,我不明白如何正确解析数据以存储特定值,假设对应于我的变量 creator 中的对象 { "XMP-dc" = {Creator = Autore } } 的值。
我做错了什么?
【问题讨论】:
标签: json swift macos xcode8 exiftool