Advertisement
junest

VAExif

Dec 2nd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.16 KB | None | 0 0
  1. import Foundation
  2. import MobileCoreServices
  3. import CoreLocation
  4. import ImageIO
  5.  
  6. class VAExif: NSObject {
  7.     var imageMetadata = NSMutableDictionary()
  8.    
  9.     private func dataFromImage(image: UIImage, metadata: NSMutableDictionary) -> NSData? {
  10.         let imageData = NSMutableData()
  11.         let mimetype = "image/jpeg"
  12.         guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimetype, nil)?.takeRetainedValue()else { return nil }
  13.         let imageDestination: CGImageDestinationRef? = CGImageDestinationCreateWithData(imageData, uti, 1, nil)
  14.         if imageDestination == nil {
  15.             return nil
  16.         } else {
  17.             CGImageDestinationAddImage(imageDestination!, image.CGImage!, metadata)
  18.             if CGImageDestinationFinalize(imageDestination!) == false {
  19.                 return nil
  20.             }
  21.         }
  22.         return imageData
  23.     }
  24.    
  25.     func convertToImageData(usingImage image: UIImage, andLocation location: CLLocation) -> NSData? {
  26.         addLocation(location)
  27.         return dataFromImage(image, metadata: imageMetadata)
  28.     }
  29.    
  30.     private func addLocation(currentLocation: CLLocation) {
  31.         var latitude: CLLocationDegrees = currentLocation.coordinate.latitude
  32.         var longitude: CLLocationDegrees = currentLocation.coordinate.longitude
  33.         var latitudeRef: String?
  34.         var longitudeRef: String?
  35.         if latitude < 0.0 {
  36.             latitude *= -1
  37.             latitudeRef = "S"
  38.         } else {
  39.             latitudeRef = "N"
  40.         }
  41.         if longitude < 0.0 {
  42.             longitude *= -1
  43.             longitudeRef = "W"
  44.         } else {
  45.             longitudeRef = "E"
  46.         }
  47.         self.gpsDictionary()[String(kCGImagePropertyGPSTimeStamp)] = self.getUTCFormattedDate(currentLocation.timestamp)
  48.         self.gpsDictionary()[String(kCGImagePropertyGPSLatitudeRef)] = latitudeRef!
  49.         self.gpsDictionary()[String(kCGImagePropertyGPSLatitude)] = Float(latitude)
  50.         self.gpsDictionary()[String(kCGImagePropertyGPSLongitudeRef)] = longitudeRef!
  51.         self.gpsDictionary()[String(kCGImagePropertyGPSLongitude)] = Float(longitude)
  52.         self.gpsDictionary()[String(kCGImagePropertyGPSDOP)] = Float(currentLocation.horizontalAccuracy)
  53.         self.gpsDictionary()[String(kCGImagePropertyGPSAltitude)] = Float(currentLocation.altitude)
  54.        
  55.     }
  56.    
  57.     private func getUTCFormattedDate(localDate: NSDate) -> String {
  58.         let dateFormatter = NSDateFormatter()
  59.         dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
  60.         return dateFormatter.stringFromDate(localDate)
  61.     }
  62.    
  63.     private func gpsDictionary() -> NSMutableDictionary {
  64.         return self.dictionaryForKey(String(kCGImagePropertyGPSDictionary))
  65.     }
  66.    
  67.     private func exifData() -> NSMutableDictionary {
  68.         return self.imageMetadata
  69.     }
  70.     private func dictionaryForKey(key: String) -> NSMutableDictionary {
  71.         if let  dict = self.imageMetadata[key] as? NSMutableDictionary {
  72.             return dict
  73.         } else {
  74.             let dict = NSMutableDictionary()
  75.             self.imageMetadata[key] = dict
  76.             return dict
  77.         }
  78.        
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement