如何为图像添加属性UIImagePicker Swift 4 [英] How to Add Properties For Image UIImagePicker Swift 4

查看:98
本文介绍了如何为图像添加属性UIImagePicker Swift 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获图像并添加经度和纬度属性,每个在StackOverflow上可用的示例都尝试了几次,但是没有用.我使用UIImagePickerController捕获图像,并使用UIImageWriteToSavedPhotosAlbum将数据保存到相册

I want to capture an image and add properties longitude and latitude, i tried several times every example available on StackOverflow but doesn't work. I use UIImagePickerController to capture image and UIImageWriteToSavedPhotosAlbum to save data to album

该属性必须如下所示

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

But now my image looks like this below

推荐答案

我不知道您到目前为止已经尝试了什么以及您的要求是什么,但是根据您的问题,这里是将location设置为image metadata.

I don't know about what you have tried so far and what is your requirement but according to your question here is an example of setting the location to image metadata.

捕获image后:

/// 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数据图像的差异:

Below is the difference b/w normal and gps data image while viewing in PhotoGallery:

documentsDirectory中,如果您看到图像信息,它将看起来像:

And in documentsDirectory if you see the image info it will look like:

这篇关于如何为图像添加属性UIImagePicker Swift 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆