Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
Add Comment
Please, Sign In to add comment