2017-05-06 98 views
1

對於一個應用程序,我試圖用我公司的所有同事解析一個vcf文件。其中一些人沒有真正的照片,而是自動地插入一張虛擬照片。現在爲了使應用程序具備未來的可用性,我不想檢查現在可以工作的500x500的分辨率。負責生成vcf的部門的想法是爲他們總是使用的虛擬照片庫文件添加評論。我想讀書,在斯威夫特,但沒有運氣,你可以在我的測試操場代碼中看到:從png文件中讀取(自定義)EXIF數據

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 

我已經嘗試過通過使用kCGImagePropertyExifU訪問用戶評論serComment,但它返回零,我想這將只返回一定的價值,如果上面的代碼也和預期一樣:

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

PNGs不包含EXIF。 PNG格式有它自己的元數據塊,exiftool可以讀取,但'CoreImage'不一定。但是通過'libpng'讀取它們並不難。 –

+0

好吧,這解釋了爲什麼我無法閱讀它們,但是如何在iOS應用程序中導入'libpng'來獲取iPhone上的元數據? –

+1

椰子樹怎麼樣? https://cocoapods.org/pods/libpng –

回答

0

下面是一個完整的示例,展示瞭如何創建一個圖像,將其保存爲帶有元數據的PNG,然後從該文件中檢索該元數據。您應該可以將其粘貼到iOS Playground中。

//: Playground - noun: a place where people can play 

import UIKit 
import ImageIO 
import MobileCoreServices 

var str = "Hello, playground" 

if let image = createImage() { 
    let pngDictionary : NSDictionary =  [ 
     kCGImagePropertyPNGTitle : "Smile for the Camera", 
     kCGImagePropertyPNGAuthor : "Smiles-R-Us", 
     kCGImagePropertyPNGCopyright : "©2017 Smiles-R-Us", 
     kCGImagePropertyPNGCreationTime : String(describing: Date()), 
     kCGImagePropertyPNGDescription : "Have a Nice Day!" 
    ] 

    let imageMetadata : NSDictionary = [ kCGImagePropertyPNGDictionary : pngDictionary ] 

    let tempURL = FileManager.default.temporaryDirectory 
    let filePath = tempURL.appendingPathComponent("Smile.png") as NSURL 

    let imageDestination = CGImageDestinationCreateWithURL(filePath, kUTTypePNG, 1, nil) 
    if let destination = imageDestination { 
     CGImageDestinationAddImage(destination, image.cgImage!, imageMetadata) 
     CGImageDestinationFinalize(destination) 
    } 

    if let imageSource = CGImageSourceCreateWithURL(filePath, nil) { 
    print (CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)) 
    } 

    print(filePath) 
} 

func createImage() -> UIImage? { 
    let bounds = CGRect(x: 0, y: 0, width: 200, height: 200) 
    UIGraphicsBeginImageContext(bounds.size) 
    if let cgContext = UIGraphicsGetCurrentContext() { 
     let inset = bounds.insetBy(dx: 20, dy: 20) 

     cgContext.clear(bounds) 
     cgContext.saveGState() 
     cgContext.setStrokeColor(UIColor.black.cgColor) 
     cgContext.setFillColor(UIColor.black.cgColor) 
     cgContext.setLineWidth(2.0) 
     cgContext.strokeEllipse(in: inset) 
     let eyeLevel = inset.maxY - (inset.height * 0.618) 
     cgContext.fillEllipse(in: CGRect(x: inset.minX + inset.width * 0.3, 
             y: eyeLevel, width: 10, height: 10)) 
     cgContext.fillEllipse(in: CGRect(x: inset.minX + inset.width * 0.6, 
             y: eyeLevel, width: 10, height: 10)) 
     cgContext.addArc(center: CGPoint(x:inset.midX, y:inset.midY), radius: (inset.width/2.0 - 20), startAngle: 2.61, endAngle: 0.52, clockwise: true) 
     cgContext.strokePath() 
    } 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return image 
} 
+0

只要我呆在操場上並立即從檔案中讀取,這確實有效。如果我將生成的Smile.png拖放到操場的ressources文件夾中,並從該文件中讀取元數據,則會剝去Xcode認爲不需要的所有字段。因此,我很遺憾地不能將其用作將照片保存在應用程序的聯繫人文件中的解決方案。 –