I am working on an application that saves user's created images to the photo library. I need to attach a user name to each saved image so that I can recall these later. As far as I understand (please correct me if I am wrong), if I am saving to the photo library, I can't change the actual file name, so a way around this is to change modify the metadata and write the user name to the description for instance. However, I am completely stuck on how to accomplish this in Swift and I have searched everywhere for an answer. The code I have to save each UIImage is as follows: I have the save option and the input for a user name presented in an alert view.
// Create the alert controller
let alertController = UIAlertController(title: "Finished?", message: "Is this your attempt?", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "Yes, save", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
if let image = self.mainImageView.image{
UIImageWriteToSavedPhotosAlbum(image,self,Selector("image:withPotentialError:contextInfo:"),nil)
}
}
let cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
self.mainImageView.image = nil
}
alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
textField.placeholder = "Enter User Name"
self.inputTextField = textField
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
And I have another function for the image, which is as follows:
func image(image: UIImage, withPotentialError error: NSErrorPointer, contextInfo: UnsafePointer<()>) {
NSLog("\(self.inputTextField.text)")
let alertController = UIAlertController(title: "Success!", message:"Your image was saved", preferredStyle: .Alert)
//Some additional code here, specific to another part of the app that has to occur after the image is saved
self.presentViewController(alertController, animated: true){}
}
This code works to save an image, however I am completely at a loss for how to add anything to the metadata of an image. If anyone has any suggestions or advice it would be greatly appreciated and please let me know if anything is unclear and I can edit or clarify in the comments.