EXIF data read and write

前端 未结 2 748
囚心锁ツ
囚心锁ツ 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:40

    Swift 4

    extension UIImage {
        func getExifData() -> CFDictionary? {
            var exifData: CFDictionary? = nil
            if let data = self.jpegData(compressionQuality: 1.0) {
                data.withUnsafeBytes {(bytes: UnsafePointer)->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
        }
    }
    

提交回复
热议问题