作为我的应用程序中的一个选项,我想让用户访问images.google.com并选择要在应用程序中使用的图像。
我已经设置了一个SFSafariViewController,但我不知道如何通过这种方式获取图像。也许我应该/可以响应复制命令?如果是这样的话,是怎么做的?我需要实现哪些类?
{ (...)
let url = URL(string: "https://images.google.com")!
let safariViewController = SFSafariViewController(url: url)
safariViewController.delegate = self
self.presentationController?.present(safariViewController, animated: true)
}
//MARK: - Ext. Delegate SFSafariViewControllerDelegate
extension ProjectImagePicker: SFSafariViewControllerDelegate {
public func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
print("SAFARI DELEGATE HIT!")
guard let image = controller.copy() as? UIImage else { return }
self.delegate?.didSelect(image: image)
controller.dismiss(animated: true, completion: nil)
}
}可以使用WKWebView来完成吗?如果可以,如何完成?
任何在正确的方向上的推动都很感谢!
发布于 2020-05-11 22:29:43
我是这样解决的:
我展示了一个Safari ViewController。当用户长按图像,然后选择共享>复制时,会将图像添加到粘贴板。有时它不是一个图像,而是一个图像URL。我处理这两种情况:
//MARK: - Ext. Delegate SFSafariViewControllerDelegate
extension ProjectImagePicker: SFSafariViewControllerDelegate {
public func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
//image was returned by Copy
if pasteboard.hasImages {
guard let image = pasteboard.image else { return }
self.delegate?.didSelect(image: image)
//Image Url was returned by Copy
} else if pasteboard.hasURLs {
guard let url = pasteboard.url else { return }
print(url)
if let data = try? Data(contentsOf: url) {
if let image = UIImage(data: data) {
self.delegate?.didSelect(image: image)
}
}
}
pasteboard.items.removeAll()
controller.dismiss(animated: true, completion: nil)
}
}看起来像预期的那样工作!
https://stackoverflow.com/questions/61683932
复制相似问题