2017-10-28 17 views
1

私はアプリの拡張機能で画像を取得しようとしています。問題は、タイプが "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) 
      } 
     }) 
    } 

私はそれが失敗した場合、URLは次のようである、loadFileRepresentation()方法から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) の結果は以下の通りです:

FYI
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] 

、これはどこで画面をキャプチャした後、共有拡張を開きます。 enter image description here

+0

おかげで、最終的に私は、画像を得ました。 https://stackoverflow.com/questions/44498932/ios-share-extension-crashes-when-sharing-from-ios-11-screenshot –

答えて

1

私はあなたのための解決策があると思います。

スクリーンショットエディタがloaditemの完了ハンドラ内で提供してUIImage

url as? UIImage 

のような画像がここを見てください:この質問へ ios swift share-extension: what are all and the best ways to handle images?

+0

ありがとう@Hilmar。あなたの答えは完璧です。画像を一度取得するのは非常に奇妙であり、それ以外の時間はURLです。 –

+0

こんにちは、はい - 私も疑問に思っています(さらに、生データの形式で3番目の方法があります)。おそらく型付きAPIがないためです。不足している機能やバグのように聞こえます。 –

関連する問題