EXIF data read and write

前端 未结 2 740
囚心锁ツ
囚心锁ツ 2020-12-08 16:58

I searched for getting the EXIF data from picture files and write them back for Swift. But I only could find predefied libs for different languages.

I also found ref

相关标签:
2条回答
  • 2020-12-08 17:39

    I'm using this to get EXIF infos from an image file:

    import ImageIO
    
    let fileURL = theURLToTheImageFile
    if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) {
        let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
        if let dict = imageProperties as? [String: Any] {
            print(dict)
        }
    }
    

    It gives you a dictionary containing various informations like the color profile - the EXIF info specifically is in dict["{Exif}"].

    0 讨论(0)
  • 2020-12-08 17:40

    Swift 4

    extension UIImage {
        func getExifData() -> CFDictionary? {
            var exifData: CFDictionary? = nil
            if let data = self.jpegData(compressionQuality: 1.0) {
                data.withUnsafeBytes {(bytes: UnsafePointer<UInt8>)->Void in
                    if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count) {
                        let source = CGImageSourceCreateWithData(cfData, nil)
                        exifData = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil)
                    }
                }
            }
            return exifData
        }
    }
    

    Swift 5

    extension UIImage {
    
        func getExifData() -> CFDictionary? {
            var exifData: CFDictionary? = nil
            if let data = self.jpegData(compressionQuality: 1.0) {
                data.withUnsafeBytes {
                    let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self)
                    if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count), 
                        let source = CGImageSourceCreateWithData(cfData, nil) {
                        exifData = CGImageSourceCopyPropertiesAtIndex(source, 0, nil)
                    }
                }
            }
            return exifData
        }
    }
    
    0 讨论(0)
提交回复
热议问题