In the previos post I tried to access location data using AssetsLibrary. However this method is depreciated, today we will try to use PHAsset from Photos framework
So the code of block below retrieves all the assets of type. Image and prints location info if available
importUIKitimportPhotosimportCoreMediaimportCoreLocationimportAddressBook//fetch all photo assets from deviceifletallAssets=PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image,options:nil){//enumerate through all assets efficiently doing the block of codeallAssets.enumerateObjectsUsingBlock({asset,index,stopin//optional unwrapping to make sure assets have what we needifletass=assetas?PHAsset{ifletlocation=ass.location{println(location)}else{println("No location for this asset")}}else{println("Error while converting to PHAsset")}})println("Finished running location getter")}else{println("Error with fetching the assets")}
Then, to display the actual address instead of location coordinates we can use CLgeodecoder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
importCoreLocationimportAddressBook//used to convert coordinates to the physical addressletgeocoder=CLGeocoder()//request Apple server to geodecode the coordinates//works async, be careful!geocoder.reverseGeocodeLocation(location){(data,error)->Voidin//get address and extract city and stateletaddress=data[0].addressDictionary!letcity=address[kABPersonAddressCityKey]asStringletstate=address[kABPersonAddressStateKey]asString//unique id for the place - need to be carefully taken care ofletplace=city+", "+stateprinln(place)}
All this works great except Apple restricts the amount of requests to the server for geodecoding. Hope that helps.
The purpose of this block of code is to print to the console the city and region where the photo, selected via standard ios image picker controller, was taken.
In this example I used ALAssets from AssetsLibrary.