自定义字符串写入和读取EXIF数据

时间:2019-03-08 08:30:20

标签: ios swift exif

我想用

编写自定义字符串EXIF数据
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    guard let imageData = photo.fileDataRepresentation() else { return }
    guard let image = UIImage(data: imageData) else { return }
    let jpeg = image.jpegData(compressionQuality: 1.0)
    var source: CGImageSource? = nil
    source = CGImageSourceCreateWithData((jpeg as CFData?)!, nil)
    let metadata = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil) as? [AnyHashable: Any]
    var metadataAsMutable = metadata
    var EXIFDictionary = (metadataAsMutable?[(kCGImagePropertyExifDictionary as String)]) as? [AnyHashable: Any]
    var GPSDictionary = (metadataAsMutable?[(kCGImagePropertyGPSDictionary as String)]) as? [AnyHashable: Any]

    if !(EXIFDictionary != nil) {
        EXIFDictionary = [AnyHashable: Any]()
    }
    if !(GPSDictionary != nil) {
        GPSDictionary = [AnyHashable: Any]()
    }

    GPSDictionary![(kCGImagePropertyGPSLatitude as String)] = 30.21313
    GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = 76.22346
    EXIFDictionary![(kCGImagePropertyExifUserComment as String)] = "Hello Image"

    let UTI: CFString = CGImageSourceGetType(source!)!
    let dest_data = NSMutableData()
    let destination: CGImageDestination = CGImageDestinationCreateWithData(dest_data as CFMutableData, UTI, 1, nil)!
    CGImageDestinationAddImageFromSource(destination, source!, 0, (metadataAsMutable as CFDictionary?))
    CGImageDestinationFinalize(destination)

    let finalImage = UIImage(data: dest_data as Data)
    self.delegate?.sendData(image: finalImage!)
    navigationController?.popViewController(animated: true)
}

为了阅读,我正在使用以下代码:

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
    }
}
print(image.getExifData())

但是它正在打印没有添加我的自定义字符串的EXIF数据。

有人以前添加过自定义字符串吗?感谢您的答复。

0 个答案:

没有答案