Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the filename of image saved to photos album

Tags:

ios

image

ios10

In modern iOS (2017),

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") }
}

The problem with this is that 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?

like image 431
Fattie Avatar asked May 04 '17 19:05

Fattie


People also ask

How do I see file names in Apple Photos?

Photos ignores the option "View > Metadata > Title" in the Photos view. But you will see the titles in smart albums or standard albums, also in Recents and Imports, if "View > Metadata > Title" is enabled. If a photo has no title, the filename will be displayed instead.

How do you save a live photo as a video on a Mac?

A Live Photo is exported as two separate files: a still-image file and a video file. In the Photos app on your Mac, select the photos you want to export. Choose File > Export > Export Unmodified Original.


1 Answers

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.

like image 128
Morty Choi Avatar answered Oct 18 '22 20:10

Morty Choi