2017-03-17 35 views
-3

如何使用UIImagePickerController無需獲取UIImage元數據。不使用UIImagePickerController獲取UIImage元數據

我基本上有一個變量UIImage,我想獲取元數據。

我需要類似「myImage.date」來訪問日期的例子。

編輯1:我嘗試這一點,但它崩潰(上EXIF VAR發現無)

func getData(image: UIImage) { 
    guard let data = UIImageJPEGRepresentation(image, 1), 
     let source = CGImageSourceCreateWithData(data as CFData, nil) else { return} 


    let imageProperties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) 
    let a = Unmanaged.passUnretained(kCGImagePropertyExifDateTimeOriginal).toOpaque() 
    let exif = CFDictionaryGetValue(imageProperties, a) 
    print(exif) 
} 
+3

哪裏是你的代碼?你有什麼嘗試?什麼不工作?你做了什麼研究?爲什麼不適合你?你會得到更多更好的答案如果你證明你已經花時間去嘗試幫助你自己了。參見[Ask] –

+0

我有我的變量UIImage,其中包含從手機拍攝的圖像,從那裏我需要獲取元數據。夥計我在網上搜索,但每個人都使用uiimagepickercontroller委託。我認爲製作視頻來證明我在google.com上的研究是無用的 – user3722523

+0

這是您需要的嗎? http://stackoverflow.com/q/40175160/2227743 – Moritz

回答

1

你可能會覺得這是一個很好的起點......

func getMetaData(forImage image: UIImage) { 
    guard let data = UIImageJPEGRepresentation(image, 1), 
     let source = CGImageSourceCreateWithData(data as CFData, nil) else { return} 

    if let type = CGImageSourceGetType(source) { 
     print("type: \(type)") 
    } 

    if let properties = CGImageSourceCopyProperties(source, nil) { 
     print("properties - \(properties)") 
    } 

    let count = CGImageSourceGetCount(source) 
    print("count: \(count)") 

    for index in 0..<count { 
     if let metaData = CGImageSourceCopyMetadataAtIndex(source, index, nil) { 
      print("all metaData[\(index)]: \(metaData)") 

      let typeId = CGImageMetadataGetTypeID() 
      print("metadata typeId[\(index)]: \(typeId)") 


      if let tags = CGImageMetadataCopyTags(metaData) as? [CGImageMetadataTag] { 

       print("number of tags - \(tags.count)") 

       for tag in tags { 

        let tagType = CGImageMetadataTagGetTypeID() 
        if let name = CGImageMetadataTagCopyName(tag) { 
         print("name: \(name)") 
        } 
        if let value = CGImageMetadataTagCopyValue(tag) { 
         print("value: \(value)") 
        } 
        if let prefix = CGImageMetadataTagCopyPrefix(tag) { 
         print("prefix: \(prefix)") 
        } 
        if let namespace = CGImageMetadataTagCopyNamespace(tag) { 
         print("namespace: \(namespace)") 
        } 
        if let qualifiers = CGImageMetadataTagCopyQualifiers(tag) { 
         print("qualifiers: \(qualifiers)") 
        } 
        print("-------") 
       } 
      } 
     } 

     if let properties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) { 
      print("properties[\(index)]: \(properties)") 
     } 
    } 
} 
+0

Thx我有很多屬性,但我沒有日期,我不知道如果我能得到UIImage的日期。也許我必須有一個PHAsset而不是UIImage? – user3722523