Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import UIKit
  2. import ImageIO
  3. import MobileCoreServices
  4.  
  5. class ExifImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  6. @IBOutlet weak var imgPic: UIImageView!
  7.  
  8. @IBAction func bActionImage(_ sender: Any) {
  9. let imagePickerController = UIImagePickerController()
  10. imagePickerController.delegate = self
  11. imagePickerController.allowsEditing = false
  12.  
  13. imagePickerController.sourceType = .camera
  14. self.present(imagePickerController, animated: true, completion: nil)
  15. }
  16.  
  17. @IBAction func save(_ sender: AnyObject) {
  18. UIImageWriteToSavedPhotosAlbum(imgPic.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
  19.  
  20. }
  21.  
  22. //MARK: - Add image to Library
  23. @objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
  24. if let error = error {
  25. // we got back an error!
  26. let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
  27. ac.addAction(UIAlertAction(title: "OK", style: .default))
  28. present(ac, animated: true)
  29. } else {
  30. let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
  31. ac.addAction(UIAlertAction(title: "OK", style: .default))
  32. present(ac, animated: true)
  33. }
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37.  
  38.  
  39.  
  40. }
  41.  
  42. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  43. let imagePick = info[UIImagePickerControllerOriginalImage] as! UIImage
  44. let jpeg = UIImageJPEGRepresentation(imagePick, 1.0)
  45.  
  46. var source: CGImageSource? = nil
  47. source = CGImageSourceCreateWithData((jpeg as CFData?)!, nil)
  48.  
  49. let metadata = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil) as? [AnyHashable: Any]
  50. var metadataAsMutable = metadata
  51.  
  52. var EXIFDictionary = (metadataAsMutable?[(kCGImagePropertyExifDictionary as String)]) as? [AnyHashable: Any]
  53. var GPSDictionary = (metadataAsMutable?[(kCGImagePropertyGPSDictionary as String)]) as? [AnyHashable: Any]
  54.  
  55. if !(EXIFDictionary != nil) {
  56. EXIFDictionary = [AnyHashable: Any]()
  57. }
  58. if !(GPSDictionary != nil) {
  59. GPSDictionary = [AnyHashable: Any]()
  60. }
  61.  
  62. GPSDictionary![(kCGImagePropertyGPSLatitude as String)] = -6.17888761051245
  63. GPSDictionary![(kCGImagePropertyGPSLongitude as String)] = 106.896323800837
  64. EXIFDictionary![(kCGImagePropertyExifUserComment as String)] = "Hello Image"
  65.  
  66. let UTI: CFString = CGImageSourceGetType(source!)!
  67. let dest_data = NSMutableData()
  68. let destination: CGImageDestination = CGImageDestinationCreateWithData(dest_data as CFMutableData, UTI, 1, nil)!
  69. CGImageDestinationAddImageFromSource(destination, source!, 0, (metadataAsMutable as CFDictionary?))
  70. CGImageDestinationFinalize(destination)
  71.  
  72. let ImgL: UIImage = UIImage(data: dest_data as Data)!
  73. imgPic.image = ImgL
  74. picker.dismiss(animated: true, completion: nil)
  75.  
  76. }
  77. }
Add Comment
Please, Sign In to add comment