您好,我已經爲我的應用程序實施了共享擴展,其中從圖庫中選取圖像併發送到特定視圖。現在的問題是,當我試圖保存圖像的陣列iOS Swift 3:將數據從共享擴展共享到主機應用程序
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")
和能得到PHOTOKEY
成功,但數組得到零,因此條件是錯誤的。 我該怎麼辦?,我GOOGLE了很多,但沒有找到任何答案
另外我沒有得到日誌,當我試圖通過調試附加擴展過程。
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()
仍然沒有運氣
我不認爲你將能夠存儲UIImages數組中userdefaults。您必須將它們轉換爲類似UIImagePNGRepresentation的數組NSData,並在啓動應用程序時再次返回。 –
Userdefaults也可能不是共享圖像的最佳方式。我沒有看到它,但像你的應用程序組的共享目錄可能會更好地工作:https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati –
我也嘗試保存在文檔目錄中,但也它也給nil –