iOS共享扩展从iOS 11共享屏幕截图时崩溃

我正在使用一个iOS应用程序,使用照片共享扩展,其中image processing和我们的主要function被触发..

在照片的应用程序模拟器这个伟大的作品。 我决定从照片的应用程序运行在设备上,它也很好,但是当我截图,并试图分享从iOS 11的新“快速截图”扩展崩溃,有什么想法?

扩展需要一个图像,将其发送到服务器,获取响应并显示该响应(全部在扩展中)。 这使我感到困扰,Messenger和Snapchat共享扩展仍然工作时,从快速截图访问!

Xcode 9也没有给我从共享扩展的任何日志。 另外值得注意的是,我使用的是每次重新安装应用程序时需要“信任”的开发者帐户。

码:

// App Group keys let suiteName = "group.suite.id" override func viewDidLoad() { print("Styling views..") styleViews() print("Styled views") print("Adding notifications..") addNotifications() print("Added notifications") print("Fetching image..") fetchSharedImage() } func styleViews(){ // Set up main view mainView.layer.cornerRadius = 8 mainShadowView.addShadow() // Set up views and buttons // Code hidden, applies shadows etc. // Code hidden, moves constraints of a view } func addNotifications(){ // Helps views tell their parent (this view controller) to navigate to another form NotificationCenter.default.addObserver(forName: NotificationDisplayFetchedLink, object: nil, queue: nil){ notification in // Handles user info in lambda block guard let userInfo = notification.userInfo, let link = userInfo["link"] as? String else { print("No userInfo found in notification") return } self.displayResult(with: link) } } func fetchSharedImage(){ // Make sure we have a valid extension item if let content = extensionContext!.inputItems[0] as? NSExtensionItem { let contentType = kUTTypeImage as String // Verify the provider is valid if let contents = content.attachments as? [NSItemProvider] { // look for images for attachment in contents { if attachment.hasItemConformingToTypeIdentifier(contentType) { attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in let url = data as! URL if let imageData = try? Data(contentsOf: url) { self.selectedImage = UIImage(data: imageData) DispatchQueue.main.async { self.selectedImageView.layer.cornerRadius = 8 self.selectedImageView.image = self.selectedImage } self.makeWebRequest() } } } } } } } func makeWebRequest(){ let url = URL(string: "url.json") let task = URLSession.shared.dataTask(with: url!) { data, response, error in guard error == nil else { return } guard let data = data else { // Data is empty return } let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary guard let dict = json as? [String:Any] else { return } let item = dict["item"] guard let itemData = item as? [[String:Any]] else { return } let link = itemData[0]["url"] NotificationCenter.default.post(name: NotificationDisplayFetchedLink, object: nil, userInfo: [link: link!]) } task.resume() } 

编辑:

所以这个解决scheme(就像Owen Zhao所说的)是iOS 11截图编辑器返回一个UIImage,而像Photos这样的应用程序将会返回URL。

我优雅地处理这个问题的解决scheme是将UIImage或者UIImage的URL保存到iOS临时目录(3天后删除),然后将该目录中的图像的URL返回到共享扩展。

let url = data as! URL if let imageData = try? Data(contentsOf: url) {

这个问题是因为这里的数据不是一个URL。 这是一个“public.image”,尝试转换为UIImage而不是数据。