Published on

Swift's Photos API Explored: Injecting GPS Data into Image EXIF Metadata


Hey there, fellow code whisperers! 🎩 πŸ‘¨β€πŸ’» Are you ready for a fun-filled, code-packed adventure into the land of photo assets? Get your keyboards warmed up, because today, we're diving into a step-by-step tutorial on how to edit saved photos' asset location and other properties using Swift's Photos API. πŸ“Έ πŸ“² Buckle up, and let's get coding! πŸš€ πŸ’»

Before we start, let's get one thing straight: What is an asset, anyway? Well, in the Photos framework, an asset represents a photo or a video. Simple enough, right? Now, onto the good stuff! πŸ“· πŸŽ₯

πŸš€ Step 1: Getting an Image from an ImageView πŸ–ΌοΈ

First things first, we need an image to play around with. Assuming you've got an ImageView in your storyboard, we'll fetch an image from it like so:

let image = yourImageView.image

Replace "yourImageView" with the name of your ImageView, and voila! You've got an image to work with. πŸŽ‰ πŸ–ΌοΈ

πŸš€ Step 2: Adding Asset Information and Other Properties πŸ“

Here's where the magic happens. We'll update the asset's location. But we're not stopping there; we're going all in! We'll also update some other properties, like the favorite status and the creation date. Check out the modified code below:

import Photos
// ...

func updatePhotoAsset(photo: UIImage) {

    let photoLibrary = PHPhotoLibrary.shared()

    // Create an album collection for your photo, if needed, you can find album too.
    guard let collection = await photoLibrary.createAlbum(albumName: "My App Name") else {

    // Save your image into Photo library first to get the asset value
    guard let asset = await photoLibrary.saveImage(photo, to: collection) else {

    // Use that asset value to perform changes on that saved photo
        let assetChangeRequest = PHAssetChangeRequest(for: asset)
        assetChangeRequest.location = currentLocation
        assetChangeRequest.isFavorite = true // Making the asset a favorite
        assetChangeRequest.creationDate = Date() // Setting the creation date to the current date
    }, completionHandler: { success, error in
        if success {
            print("Properties were successfully set")
        } else if let error = error {
            print("Failed to set properties: \(error)")

With this code, you're not just setting the location of the asset, but also marking it as a favorite and updating its creation date. Talk about multitasking! πŸ’ͺπŸ‘©β€πŸ’»

πŸš€ Step 3: Exploring Other Important Methods in the Photos API πŸ—ΊοΈ

The Photos API is like a treasure chest full of handy methods. Two other important ones are requestContentEditingInput(with:completionHandler:) and contentEditingOutput(with:).

requestContentEditingInput(with:completionHandler:) prepares your asset for editing. It gives you access to a PHContentEditingInput object, which you can use to get the image or video data, along with metadata and adjustment data.

contentEditingOutput(with:) on the other hand, is used to write the changes you've made to the photo back to the Photos library.

These two methods together form the backbone of editing photos with the Photos API. Definitely worth exploring! πŸ§πŸ“š

And there you have it, folks! A step-by-step guide to editing photo assets like a pro. Remember, practice makes perfect, so don't be shy to get your hands dirty and play around with the code. Happy coding, and until next time, keep on rocking the digital world! πŸ€˜πŸ’»πŸŒ