Search code examples
iosimagegpslocation

How to write CLLocation's GPS information into UIImage using PHPhotoKit?


Is there any method to write GPS info into one image? for example, I want to develop a camera app that can take photos with location information. Can I use PhotoKit related methods or some other methods such as ImageIO? Thanks.


Solution

  • You can add location information to your image like this

    let locationCordinate = CLLocationCoordinate2DMake(lat, long)
    let currentDate = NSDate()
    let imageLocation = CLLocation(coordinate: locationCordinate, altitude: 0.0, horizontalAccuracy: 0.0, verticalAccuracy: 0.0, timestamp: currentDate)
    

    After this you can add this location to your image like this

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
    
          let assetLocationChangeRequest = PHAssetChangeRequest(forAsset: lastImageAsset)
          assetLocationChangeRequest.location = imageLocation
    
          }, completionHandler: {
                (success:Bool, error:NSError?) -> Void in
    
                if (success) {
                    print("Succesfully saved location to image")
                } else {
                    print("Failed to save with Error : \(error!)")
                    }
            })