Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import MobileCoreServices
- import CoreLocation
- import ImageIO
- class VAExif: NSObject {
- var imageMetadata = NSMutableDictionary()
- private func dataFromImage(image: UIImage, metadata: NSMutableDictionary) -> NSData? {
- let imageData = NSMutableData()
- let mimetype = "image/jpeg"
- guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimetype, nil)?.takeRetainedValue()else { return nil }
- let imageDestination: CGImageDestinationRef? = CGImageDestinationCreateWithData(imageData, uti, 1, nil)
- if imageDestination == nil {
- return nil
- } else {
- CGImageDestinationAddImage(imageDestination!, image.CGImage!, metadata)
- if CGImageDestinationFinalize(imageDestination!) == false {
- return nil
- }
- }
- return imageData
- }
- func convertToImageData(usingImage image: UIImage, andLocation location: CLLocation) -> NSData? {
- addLocation(location)
- return dataFromImage(image, metadata: imageMetadata)
- }
- private func addLocation(currentLocation: CLLocation) {
- var latitude: CLLocationDegrees = currentLocation.coordinate.latitude
- var longitude: CLLocationDegrees = currentLocation.coordinate.longitude
- var latitudeRef: String?
- var longitudeRef: String?
- if latitude < 0.0 {
- latitude *= -1
- latitudeRef = "S"
- } else {
- latitudeRef = "N"
- }
- if longitude < 0.0 {
- longitude *= -1
- longitudeRef = "W"
- } else {
- longitudeRef = "E"
- }
- self.gpsDictionary()[String(kCGImagePropertyGPSTimeStamp)] = self.getUTCFormattedDate(currentLocation.timestamp)
- self.gpsDictionary()[String(kCGImagePropertyGPSLatitudeRef)] = latitudeRef!
- self.gpsDictionary()[String(kCGImagePropertyGPSLatitude)] = Float(latitude)
- self.gpsDictionary()[String(kCGImagePropertyGPSLongitudeRef)] = longitudeRef!
- self.gpsDictionary()[String(kCGImagePropertyGPSLongitude)] = Float(longitude)
- self.gpsDictionary()[String(kCGImagePropertyGPSDOP)] = Float(currentLocation.horizontalAccuracy)
- self.gpsDictionary()[String(kCGImagePropertyGPSAltitude)] = Float(currentLocation.altitude)
- }
- private func getUTCFormattedDate(localDate: NSDate) -> String {
- let dateFormatter = NSDateFormatter()
- dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
- return dateFormatter.stringFromDate(localDate)
- }
- private func gpsDictionary() -> NSMutableDictionary {
- return self.dictionaryForKey(String(kCGImagePropertyGPSDictionary))
- }
- private func exifData() -> NSMutableDictionary {
- return self.imageMetadata
- }
- private func dictionaryForKey(key: String) -> NSMutableDictionary {
- if let dict = self.imageMetadata[key] as? NSMutableDictionary {
- return dict
- } else {
- let dict = NSMutableDictionary()
- self.imageMetadata[key] = dict
- return dict
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement