Skip to content

Instantly share code, notes, and snippets.

@kkleidal
Last active August 18, 2016 12:00
Show Gist options
  • Save kkleidal/73401405f7d5fd168d061ad0c154ea18 to your computer and use it in GitHub Desktop.
Save kkleidal/73401405f7d5fd168d061ad0c154ea18 to your computer and use it in GitHub Desktop.
Strip location/date metadata from photo library photo asset imported with FDTake
private var xifDateFormatter: NSDateFormatter = { () -> NSDateFormatter in
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy:MM:dd' 'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
return dateFormatter
}()
// ...
fdTakeController.didGetPhoto = { (photo: UIImage, info: [NSObject : AnyObject]) -> Void in
dispatch_async(dispatch_get_main_queue()) {
let dictionary = info as! [String : AnyObject]
if let url = dictionary["UIImagePickerControllerReferenceURL"] as? NSURL {
// Find asset in photo library:
let asset = PHAsset.fetchAssetsWithALAssetURLs([url], options: nil).firstObject as? PHAsset
if asset != nil {
let options = PHContentEditingInputRequestOptions()
options.networkAccessAllowed = true
// Load metadata for asset:
asset!.requestContentEditingInputWithOptions(options) { (contentEditingInput: PHContentEditingInput?, _) -> Void in
let fullImage = CIImage(contentsOfURL: contentEditingInput!.fullSizeImageURL!)
var date: NSDate? = nil
// Parse the date from the EXIF data:
if let dateTimeString = fullImage!.properties["{Exif}"]?["DateTimeOriginal"] as? String {
date = xifDateFormatter.dateFromString(dateTimeString)
}
// Parse the 2D location from the GPS data:
var location: CLLocation? = nil
if let gps = fullImage?.properties["{GPS}"] {
var latitude: CLLocationDegrees? = nil
if let latitudeRaw = gps["Latitude"] as? CLLocationDegrees, latitudeRef = gps["LatitudeRef"] as? String {
let sign = ((latitudeRef == "N") ? 1 : -1)
latitude = CLLocationDegrees(Double(sign) * Double(latitudeRaw))
}
var longitude: CLLocationDegrees? = nil
if let longitudeRaw = gps["Longitude"] as? CLLocationDegrees, longitudeRef = gps["LongitudeRef"] as? String {
let sign = ((longitudeRef == "E") ? 1 : -1)
longitude = CLLocationDegrees(Double(sign) * Double(longitudeRaw))
}
if latitude != nil && longitude != nil {
location = CLLocation(latitude: latitude!, longitude: longitude!)
}
}
self.captionPhoto(ImageWithMeta(photo: photo, location: location, date: date))
}
} else {
self.captionPhoto(ImageWithMeta(photo: photo, location: nil, date: nil))
}
} else {
self.captionPhoto(ImageWithMeta(photo: photo, location: nil, date: nil))
}
}
}
@GiriVertisize
Copy link

Hi , I am using IOS 9.3,So for that i want to access the location from photos.I am using PHAssets.So is there any source code to get the location meta data of selected image from photos album.Please help me.I am very new to swift.

Thanks in Advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment