2017-03-17 79 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