ios - 如何为图像 UIImagePicker Swift 4 添加属性

标签 ios swift gps uiimagepickercontroller exif

我想捕捉图像并添加属性经度和纬度,我尝试了好几次 StackOverflow 上可用的每个示例,但都不起作用。我使用 UIImagePickerController 捕获图像,使用 UIImageWriteToSavedPhotosAlbum 将数据保存到相册

属性必须是这样的

enter image description here

但是现在我的图片如下所示

enter image description here

最佳答案

我不知道你到目前为止尝试了什么,你的要求是什么,但根据你的问题,这里有一个将 location 设置为 image metadata 的例子.

捕获图像后:

/// This is Mohali location for example
let location = CLLocation(latitude: 30.7046, longitude: 76.7179)

/// It will return image metaData including location
let metaData = self.addLocation(location, toImage: image)

/// Saving the image to gallery
self.saveImage(image, withMetadata: metaData)

这里是所有必需的方法:

func addLocation(_ location: CLLocation, toImage image: UIImage) -> Dictionary<String, Any> {

    /// Initializing the metaData dict
    var metaData: Dictionary<String, Any> = [:]

    /// Check if image already have its meta data
    if let ciImage = image.ciImage {
        metaData = ciImage.properties
    }

    /// Initializing the gpsData dict
    var gpsData: Dictionary<String, Any> = [:]

    /// Check if there is any gps information
    if let gps = metaData[kCGImagePropertyGPSDictionary as String] as? Dictionary<String, Any> {
        gpsData = gps
    }

    /// Adding all the required information to gpsData dictionary
    // #1. Data & Time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
    let localDate = dateFormatter.string(from: location.timestamp)
    gpsData[kCGImagePropertyGPSTimeStamp as String] = localDate

    // #2. Latitude, Longitude
    var latitude  = location.coordinate.latitude
    var longitude = location.coordinate.longitude
    var latRef = ""
    var lngRef = ""
    if latitude < 0.0 {
        latitude *= -1.0
        latRef = "S"
    } else  {
        latRef = "N"
    }

    if longitude < 0.0 {
        longitude *= -1.0
        lngRef = "W"
    }
    else {
        lngRef = "E"
    }

    gpsData[kCGImagePropertyGPSLatitudeRef as String] = latRef
    gpsData[kCGImagePropertyGPSLongitudeRef as String] = lngRef
    gpsData[kCGImagePropertyGPSLatitude as String] = latitude
    gpsData[kCGImagePropertyGPSLongitude as String] = longitude

    // #3. Accuracy
    gpsData[kCGImagePropertyGPSDOP as String] = location.horizontalAccuracy

    // #4. Altitude
    gpsData[kCGImagePropertyGPSAltitude as String] = location.altitude

    /// You can add what more you want to add into gpsData and after that
    /// Add this gpsData information into metaData dictionary
    metaData[kCGImagePropertyGPSDictionary as String] = gpsData

    return metaData
}

func saveImage(_ image:UIImage, withMetadata metaData: Dictionary<String, Any>) {
    /// Creating jpgData from UIImage (1 = original quality)
    guard let jpgData = UIImageJPEGRepresentation(image, 1) else { return }

    /// Adding metaData to jpgData
    guard let source = CGImageSourceCreateWithData(jpgData as CFData, nil), let uniformTypeIdentifier = CGImageSourceGetType(source) else {
        return
    }

    let finalData = NSMutableData(data: jpgData)
    guard let destination = CGImageDestinationCreateWithData(finalData, uniformTypeIdentifier, 1, nil) else { return }
    CGImageDestinationAddImageFromSource(destination, source, 0, metaData as CFDictionary)
    guard CGImageDestinationFinalize(destination) else { return }

    /// Your destination file path
    let filePath = "\(documentsDicrectoryPath)/finalImage.jpg"

    /// Now write this image to directory
    if FileManager.default.fileExists(atPath: filePath) {
        try? FileManager.default.removeItem(atPath: filePath)
    }

    let success = FileManager.default.createFile(atPath: filePath, contents: finalData as Data, attributes: [FileAttributeKey.protectionKey : FileProtectionType.complete])
    if success {
        /// Finally Save image to Gallery
        /// Important you need PhotoGallery permission before performing below operation.
        try? PHPhotoLibrary.shared().performChangesAndWait {
            PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: URL(fileURLWithPath: filePath))
        }
    }
}

下面是在 PhotoGallery 中查看时黑白正常图像和 gps 数据图像的区别:

Before After

如果您在 documentsDirectory 中看到图像信息,它将如下所示:

Image Get Info in documents directory

关于ios - 如何为图像 UIImagePicker Swift 4 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52196860/

相关文章:

swift - 如何保存用户位置并使用图钉(或其他标记)将其显示在 map 上?

ios - UITableViewCell带有自调整大小标签的扩展动画,视觉动画问题

ios - 带有 "Bearer Token"的 Twitter 仅应用程序身份验证

ios - iPhone 查看照片 - 向任意方向滑动

ios - 在 Google map 中显示带有独特数据的自定义标记

arrays - 在数组的开头插入一个字符串,而不丢失第一个位置的字符串

ios - 当我输入时 UISearchController 不检索用户

gps - 近似和内插 GPS 轨迹

iphone - 使用过的 iPhone 应用程序测试 - 基础知识

ios - 如何在数据库中的导航栏上显示标题