【问题标题】:Retrieve image path saved by UIActivityViewController检索 UIActivityViewController 保存的图像路径
【发布时间】:2021-03-23 03:00:52
【问题描述】:

用户可以使用UIActivityViewController sheet 将 UIImageView 上的图像保存到库中,也可以分享或发布到社交媒体,如下所示:

如果用户选择“保存图像”,UIActivityViewController 无法返回保存的图像路径。它只会默默地保存和关闭。

我如何检索保存它的路径?我将存储访问已保存图像的路径并将它们显示在另一个视图中。非常感谢您的帮助。

编辑:下面是当用户点击分享按钮时我呈现 UIActivityViewController 的方式。

let imageShare = [ self.imageView.image ]
let activityViewController = UIActivityViewController(activityItems: imageShare as [Any] , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = self.shareButton.frame

self.present(activityViewController, animated: true, completion: nil)

【问题讨论】:

  • 请同时添加您的代码!你是如何使用 UIActivityViewController 的?
  • 我添加了,谢谢指出。

标签: ios swift filepath uiactivityviewcontroller


【解决方案1】:

我认为问题是:既然你已经有了图像......为什么你想得到那个路径?

但是 - 您可以在 UIActivityViewController 完成中获得选定的“分享”活动:

func shareImage(_ img: UIImage) -> Void {
    
    let items = [img]
    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
    
    activityViewController.popoverPresentationController?.sourceView = self.view
    activityViewController.popoverPresentationController?.sourceRect = self.shareButton.frame

    activityViewController.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed:
                                        Bool, arrayReturnedItems: [Any]?, error: Error?) in
        if completed {
            if let act = activityType {
                switch act {
                case .saveToCameraRoll:
                    print("Save Image was selected")
                    // do something here
                    // such as getting the asset reference?
                    //  or getting the URL to the asset?
                
                case .copyToPasteboard:
                    print("Copy was selected")
                    
                default:
                    print("Something else...")
                }
            }
        } else {
            print("Cancel was selected")
        }
        if let shareError = error {
            print("error while sharing: \(shareError.localizedDescription)")
        }

    }
    
    present(activityViewController, animated: true)
    
}

因此,根据您的实际需求,您可以轻松找到示例代码,用于从相机胶卷(或其资产参考或 URL 等)中获取最近保存的图像。

【讨论】:

  • 感谢您的回复。我实际上知道我们可以在保存图像时访问已完成的活动类型,但我不确定如何获取该图像的路径。访问最近保存的图像可能不是检索它的准确方法,因为同时可能有一些其他图像被保存到库中;技术上可行。
  • @UmitKaya - 那么,我想,问题仍然存在......为什么?如果你能可靠地得到它,你打算如何处理路径参考?
  • 我想检索那些保存的图片并在其他视图中显示给用户作为保存的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 2011-06-20
  • 1970-01-01
相关资源
最近更新 更多