【问题标题】:cannot open/read image via share extension无法通过共享扩展打开/读取图像
【发布时间】:2017-10-28 08:21:40
【问题描述】:

我正在尝试在应用扩展中获取图像。问题是当类型为“public.image”时我无法获取图像,但是当类型为“public.png”时它成功。我想要做的只是获取图像并上传到服务器。

这是我正在使用的代码。

if let extensionContext = self.extensionContext,
        let item = extensionContext.inputItems.first as? NSExtensionItem,
        let attachment = item.attachments?.first as? NSItemProvider {

        guard let type: String = attachment.registeredTypeIdentifiers.first else { return } 
        // -> "public.png" or "public.image"
        attachment.loadFileRepresentation(forTypeIdentifier: type, completionHandler: { (url, error) in
            guard let url = url else { return }
            do {
                let imageData = try Data(contentsOf: url)
                if let image = UIImage(data: imageData) {
                    // some task with image
                } else {
                    // reach here!!!!!
                }
            } catch {
                print(error.localizedDescription)
            }
        })
    }

我从loadFileRepresentation() 方法检查了url,失败时的url 是这样的: file:///private/var/mobile/Containers/Data/PluginKitPlugin/E82D4A01-A2ED-42E0-9378-420F5B1DBAD3/tmp/.com.apple.Foundation.NSItemProvider.5FKOVj/image

和结果 FileManager.default.attributesOfItem(atPath: url.path) 如下:

attributesOfFile: [__C.FileAttributeKey(_rawValue: NSFileOwnerAccountName): mobile,
 __C.FileAttributeKey(_rawValue: NSFilePosixPermissions): 420,
 __C.FileAttributeKey(_rawValue: NSFileSystemNumber): 16777219,
 __C.FileAttributeKey(_rawValue: NSFileReferenceCount): 1,
 __C.FileAttributeKey(_rawValue: NSFileSystemFileNumber): 1163851, 
 __C.FileAttributeKey(_rawValue: NSFileCreationDate): 2017-10-28 07:36:09 +0000,
 __C.FileAttributeKey(_rawValue: NSFileProtectionKey): NSFileProtectionCompleteUntilFirstUserAuthentication,
 __C.FileAttributeKey(_rawValue: NSFileType): NSFileTypeRegular, __C.FileAttributeKey(_rawValue: NSFileGroupOwnerAccountName): mobile,
 __C.FileAttributeKey(_rawValue: NSFileGroupOwnerAccountID): 501,
 __C.FileAttributeKey(_rawValue: NSFileModificationDate): 2017-10-28 07:36:09 +0000,
 __C.FileAttributeKey(_rawValue: NSFileSize): 1018925,
 __C.FileAttributeKey(_rawValue: NSFileExtensionHidden): 0,
 __C.FileAttributeKey(_rawValue: NSFileOwnerAccountID): 501]

仅供参考,这是我在捕获屏幕后打开共享扩展的地方。

【问题讨论】:

标签: ios iphone ios-app-extension ios8-share-extension


【解决方案1】:

我想我有一个适合你的解决方案。

屏幕截图编辑器在加载项的完成处理程序中提供图像作为 UIImage

url as? UIImage

请看这里: ios swift share-extension: what are all and the best ways to handle images?

【讨论】:

  • 谢谢@Hilmar。你的回答很完美。一次获取图片,另一次获取url,这很奇怪。
  • 嗨,是的 - 我也想知道(即使除了原始数据形式还有第三种方式)。可能是因为目前没有类型化的 API。听起来像是缺少功能或错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多