且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取保存到相册的图像文件名

更新时间:2021-07-14 08:27:33

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