获取保存到相册的图像文件名 [英] Get the filename of image saved to photos album

查看:164
本文介绍了获取保存到相册的图像文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个看似简单的问题获得巨额赏金。

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

在现代iOS(2017)中,

In modern iOS (2017),

这里实际上是我知道的唯一方法将图像保存到iOS照片系统,并获取文件名/路径。

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

这有两个问题:

这是非常笨拙的,而且在很多方面看起来很麻烦。

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

今天真的要走了吗?

推荐答案

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

我认为你可以用 PHPhotoLibrary来做到这一点 PHObjectPlaceholder

这篇关于获取保存到相册的图像文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆