将exif数据写入图像使用相机快速4

时间:2018-07-18 06:22:35

标签: ios swift camera alamofire exif

如何使用相机将exif数据写入图像, 我已经尝试过下面的方法,但是没有成功。 首先拍摄一张照片,然后以经度和纬度使用jpeg或png格式添加元数据exif。我正在尝试从图像样本缓冲区中保存一些元数据以及图像。

import UIKit
import ImageIO 
import MobileCoreServices

class ExifImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var imgPic: UIImageView!

    @IBAction func bActionImage(_ sender: Any) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = false

        imagePickerController.sourceType = .camera
        self.present(imagePickerController, animated: true, completion: nil)
    }

    @IBAction func save(_ sender: AnyObject) {
        UIImageWriteToSavedPhotosAlbum(imgPic.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

    }

    //MARK: - Add image to Library
    @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            // we got back an error!
            let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        } else {
            let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()



    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let imagePick = info[UIImagePickerControllerOriginalImage] as! UIImage
        let jpeg = UIImageJPEGRepresentation(imagePick, 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)] = -6.17888761051245
        GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = 106.896323800837
        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 ImgL: UIImage = UIImage(data: dest_data as Data)!
        imgPic.image = ImgL
        picker.dismiss(animated: true, completion: nil)

    }
}

我正在尝试从图像样本缓冲区中保存一些元数据以及图像。

0 个答案:

没有答案