从png文件中读取(自定义)EXIF数据

时间:2017-05-06 11:34:51

标签: ios swift swift3

对于一个应用,我试图与我公司的所有同事解析一个vcf文件。他们中的一些没有真正的照片,而是自动插入虚拟照片。现在,为了使应用程序面向未来,我不想检查500x500的分辨率,现在可以使用。负责vcf生成的部门的想法是为他们总是使用的虚拟照片库文件添加注释。我尝试在Swift中阅读,但没有运气,因为你可以在我的测试游乐场代码中看到:

import UIKit
import ImageIO

let photo = UIImage(named: "bild")!
let photoData = UIImagePNGRepresentation(photo)!
let base64String = photoData.base64EncodedString()

let photoSource = CGImageSourceCreateWithData(photoData as CFData, nil)!
for (key, value) in CGImageSourceCopyPropertiesAtIndex(photoSource, 0, nil) as! [String : Any] {
    print("\(key): \(value)")
}

输出:

PixelWidth: 500
Depth: 8
ProfileName: sRGB IEC61966-2.1
HasAlpha: 1
ColorModel: RGB
{PNG}: {
    Chromaticities =     (
        "0.3127",
        "0.329",
        "0.64",
        "0.33",
        "0.3",
        "0.6000000000000001",
        "0.15",
        "0.06"
    );
    Gamma = "0.45455";
    InterlaceType = 0;
    sRGBIntent = 0;
}
PixelHeight: 500

同时在终端中exiftool的输出会在同一张图片上显示此内容(特别参见User CommentDocument Name(自定义字段):

➔ exiftool bild.png
ExifTool Version Number         : 10.50
File Name                       : bild.png
Directory                       : .
File Size                       : 4.2 kB
File Modification Date/Time     : 2017:05:06 12:51:23+02:00
File Access Date/Time           : 2017:05:06 12:51:24+02:00
File Inode Change Date/Time     : 2017:05:06 12:51:23+02:00
File Permissions                : rw-r--r--
File Type                       : PNG
File Type Extension             : png
MIME Type                       : image/png
Image Width                     : 500
Image Height                    : 500
Bit Depth                       : 8
Color Type                      : Palette
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Noninterlaced
Palette                         : (Binary data 477 bytes, use -b option to extract)
Transparency                    : 0
Background Color                : 0
Pixels Per Unit X               : 2835
Pixels Per Unit Y               : 2835
Pixel Units                     : meters
Modify Date                     : 2017:05:05 08:04:36
Exif Byte Order                 : Big-endian (Motorola, MM)
Document Name                   : dummy
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Y Cb Cr Positioning             : Centered
Exif Version                    : 0231
Components Configuration        : Y, Cb, Cr, -
User Comment                    : dummy
Flashpix Version                : 0100
Color Space                     : Uncalibrated
Image Size                      : 500x500
Megapixels                      : 0.250

我已经尝试使用kCGImagePropertyExifUserComment访问用户注释,但是这会返回nil,我猜它只返回一些值,如果上面的代码也按预期工作:

let userComment = dict[kCGImagePropertyExifUserComment as String] // User Comment is set --> but this returns nil
let pixelWidth = dict[kCGImagePropertyPixelWidth as String] // As a reference that this does normally work --> shows 500 as expected

您对如何使用Swift代码可读取的图片添加评论有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这是一个完整的示例,展示如何创建图像,将其另存为带元数据的PNG,然后从文件中检索元数据。您应该可以将其粘贴到iOS Playground中。

UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname

UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE ProductName = :prodname