Get the filename of image saved to photos album

Gain a huge bounty with this seemingly easy question that has no attention.

In modern iOS (2017),

  • Go vs. return button in iOS keyboard for HTML input forms
  • No such module while importing my custom framework to the project
  • who is issuing my UIKeyboardDidHideNotification?
  • Turn off Zooming in UIScrollView
  • UISearchController doesn't work properly with a non-translucent UINavigationBar
  • Developing iOS app on Windows
  • here’s actually the only way I know to save an image to the iOS photos system, and get the filename/path.

    import UIKit
    import Photos
    
    func saveTheImage... () {
    
        UIImageWriteToSavedPhotosAlbum(yourUIImage, self,
            #selector(Images.image(_:didFinishSavingWithError:contextInfo:)),
            nil)
    }
    
    func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        guard error == nil else {
            print("Couldn't save the image!")
            return
        }
        doGetFileName()
    }
    
    func doGetFileName() {
        let fo: PHFetchOptions = PHFetchOptions()
        fo.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let r = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fo)
        if let mostRecentThingy = r.firstObject {
    
            PHImageManager.default().requestImageData(
                for: mostRecentThingy,
                options: PHImageRequestOptions(),
                resultHandler: { (imagedata, dataUTI, orientation, info) in
    
                    if info!.keys.contains("PHImageFileURLKey") {
                        let path = info!["PHImageFileURLKey"] as! NSURL
    
                        print("Holy cow. The path is \(path)")
                    }
                    else { print("bizarre problem") }
                })
    
        }
        else { print("unimaginable catastrophe") }
    }
    

    There are two problems with this:

    1. WTH ?!?

    2. It fails in racetrack conditions.

    This is amazingly unwieldy, and it seems worrysome in a number of ways.

    Is it really the way to go, today?

    4 Solutions Collect From Internet About “Get the filename of image saved to photos album”

    You just saved image programmatically, so you can get the image from camera and save it with your path:

    //save image in Document Derectory
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSLog(@"Get Path : %@",documentsDirectory);
    
          //create Folder if Not Exist
          NSError *error = nil;
          NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/YourFolder"];
    
          if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
    
          NSString *yourPhotoName=@"YourPhotoName";
          NSString* path= [dataPath stringByAppendingString:[NSString stringWithFormat:@"/%@.png",yourPhotoName]];
          NSData* imageData = UIImagePNGRepresentation(imageToSaved); //which got from camera
    
          [imageData writeToFile:path atomically:YES];
    
          imagePath = path;
          NSLog(@"Save Image Path : %@",imagePath);
    
    extension PHPhotoLibrary {
    
        func save(imageData: Data, withLocation location: CLLocation?) -> Promise<PHAsset> {
            var placeholder: PHObjectPlaceholder!
            return Promise { fullfil, reject in
                performChanges({
                    let request = PHAssetCreationRequest.forAsset()
                    request.addResource(with: .photo, data: imageData, options: .none)
                    request.location = location
                    placeholder = request.placeholderForCreatedAsset
                }, completionHandler: { (success, error) -> Void in
                    if let error = error {
                        reject(error)
                        return
                    }
    
                    guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: .none).firstObject else {
                        reject(NSError())
                        return
                    }
    
                    fullfil(asset)
                })
            }
        }
    }
    

    I think you can do this with PHPhotoLibrary and PHObjectPlaceholder.

    If you want to get the filename just open the photoAlbum like this for example:

    if iOS 10 + (please set this key in info.plist : Privacy – Photo Library Usage Description)

    func openLibrary() {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        } else {
            //alertWithTitle:"No gallery" message:"Unfortunately, we cannot access your photo gallery."
        }
    }
    

    then just conform to the protocol and implement this method:

    class ViewController: UIImagePickerControllerDelegate {
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
             if let url = info[UIImagePickerControllerReferenceURL] as? URL {
    
                url.absoluteURL
                //or
                url.absoluteString
    
             }
             picker.dismiss(animated: true, completion: nil)
        }
    }
    

    hope this will help you,
    wish you all the best.

    Maybe this is a different approach but here’s what I’m doing in my app and I’m satisfied with it:

    func saveImage(image: UIImage, name: String) {
    
        var metadata = [AnyHashable : Any]()
        let iptcKey = kCGImagePropertyIPTCDictionary as String
        var iptcMetadata = [AnyHashable : Any]()
    
        iptcMetadata[kCGImagePropertyIPTCObjectName as String] = name
        metadata[iptcKey] = iptcMetadata
    
        let library = ALAssetsLibrary()
    
        library.writeImage(toSavedPhotosAlbum: image.cgImage, metadata: metadata) { url, error in
    
            // etc...
        }
    }
    

    If you don’t want to use ALAssetsLibrary, you’ll probably be interested in this answer.