I have a brand new iOS app that generates images and lets the users save them into the Camera SavedPhotosAlbum. However, I wanna do something like Snapchat and Frontback, and save these images also to a custom-named album.
So this is my code right now:
let imageToSave = self.currentPreviewImage
let softwareContext = CIContext(options:[kCIContextUseSoftwareRenderer: true])
let cgimg = softwareContext.createCGImage(imageToSave, fromRect:imageToSave.extent())
ALAssetsLibrary().writeImageToSavedPhotosAlbum(cgimg, metadata:imageToSave.properties(), completionBlock:nil)
I've seen a few examples of people doing this in Objective-C but nothing that I could translate to Swift, and I've check the writeImageToSavedPhotosAlbum
method signatures and none of them seem to allow saving to a custom album.
Thanks, was trying to use this code, but found some logic errors. Here is the cleaned up code
Even after the fixes, my PhotoAlbum still didn't work for the first image and if I wanted to save more than one images at once I've ended up with multiple empty albums. So I've upgraded the class and I only save the emoji after the album has been created.
New version:
}
If you want to save multiple images at once, here is my code for that. The key here is to delay the saving of the other images which are not the first, because we have to create the album first. (otherwise we end up with duplicate albums, because all the saving processes will try to create a Custom Album). This is the code from my app, so you can understand the logic:
For those of you looking for a one-function solution using Swift 4, I have condensed some of the above code into a function that simply takes in a UIImage, String-type album name, and a callback indicating success/failure.
Note: this function is more complex so it will obviously have a slower runtime than the previous solutions, but I have posted it here for other peoples' convenience.
If you're interested in a protocol oriented approach that allows for simple saving to multiple albums with different names that's up to date with Swift 4 and avoids singleton use then read on.
This approach checks for and obtains user authorization, checks for or creates the photo album then saves the image to the requested album. If at any point an error is triggered the completion block is run with the corresponding error.
An upside of this approach is that the user is not prompted for photos access as soon as the instance is created, instead they are prompted when they are actually trying to save their image, if authorization is required.
This method also allows you to define a very simple class that encapsulates a photo album, conforming to the PhotoAlbumHandler protocol and thus getting all the photo album interaction logic for free, like this:
You can also then create an enum that manages and encapsulates all your photo albums. Adding support for another album is as simple as adding a new case to the enum and defining the corresponding albumName.
Like this:
Using this approach makes managing your photo albums a breeze, in your viewModel (or view controller if you're not using view models) you can create references to your albums like this:
Then to save an image to one of the albums you could do something like this:
For error handling I opted to encapsulate any possible errors in an error enum:
The protocol that defines the interface and the protocol extension that handles interaction with the system Photo Album functionality is here:
If you'd like to look through a sample Xcode project you can find one here: https://github.com/appteur/ios_photo_album_sample
I found that some proposed solutions here were working but I wanted to rewrite a reusable version of it. Here is how you use it:
When saving an image, it request the user's photo access (which returns immediately if previously authorized) and tries to create an album if one doesn't exist yet. Below is the full source code written in Swift 3 and compatible with Objective-C.
Improved upon @Damien answer. Works with
UIImage
and video (with url) too.Swift4
tested:Usage:
or