你好,我已经为我的应用程序实现了共享扩展,在该应用程序中,从图库中选择图像并发送到特定视图。现在的问题是当我试图保存图像数组(从图片库中提取的图像)时。
func manageImages() {
let content = extensionContext!.inputItems[0] as! NSExtensionItem
let contentType = kUTTypeImage as String
for (index, attachment) in (content.attachments as! [NSItemProvider]).enumerated() {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in
if error == nil, let url = data as? URL {
do {
let imageData = try Data(contentsOf: url)
let image = UIImage(data: imageData)
self.selectedImages.append(image!)
if index == (content.attachments?.count)! - 1 {
self.imgCollectionView.reloadData()
UserDefaults.standard.set(self.selectedImages, forKey: "PHOTOKEY")
UserDefaults.standard.synchronize()
}
}
catch let exp {
print("GETTING ERROR \(exp.localizedDescription)")
}
} else {
print("GETTING ERROR")
let alert = UIAlertController(title: "Error", message: "Error loading image", preferredStyle: .alert)
let action = UIAlertAction(title: "Error", style: .cancel) { _ in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
}
}
}并在AppDelegate方法中获取该数组
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let key = url.absoluteString.components(separatedBy: "=").last, let _ = UserDefaults.standard.array(forKey: key) {
let myVC = UIStoryboard.getViewController(storyboardName: "Main",
storyboardId: "MyViewController")
let navVC = UIStoryboard.getViewController(storyboardName: "Main",
storyboardId: "MyNavigationVC") as! UINavigationController
navVC.viewControllers.append(myVC)
self.window?.rootViewController = navVC
self.window?.makeKeyAndVisible()
return true
}
return false
}我正在发送url let url = URL(string: "unfoldsPrintsShare://dataUrl=PHOTOKEY"),并能得到光通信
成功,但数组获得零,因此条件是假的。我应该怎么做?,我搜索了很多,但没有找到任何答案。
而且,当我试图通过调试附加扩展进程时,我不会获得日志。
P.S.:使用Xcode 8.3.3 iOS 10.3,iPhone 6物理设备
更新:尝试通过应用程序组设置名称
let userDefaults = UserDefaults(suiteName: "group.com.company.appName")
userDefaults?.set(self.selectedImages, forKey: "PHOTOKEY")
userDefaults?.synchronize()还是没有运气
发布于 2017-08-27 04:21:06
据斯特凡·丘奇说,我试过这样做
let imagesData: [Data] = []将图像Data格式保存为数组而不是UIImage格式
let userDefaults = UserDefaults(suiteName: "group.com.myconmanyName.AppName")
userDefaults?.set(self?.imagesData, forKey: key)
userDefaults?.synchronize()而且起作用了
谢谢斯特凡教堂
https://stackoverflow.com/questions/45897547
复制相似问题