5

I'm trying to get image in the app extension. The problem is I can't get the image when the type is "public.image", however, it succeeds when the type is "public.png". What I want to do is just getting the image and upload to the server.

Here's the code I'm using.

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)
            }
        })
    }

I inspected the url from loadFileRepresentation() method, the url is like when it fails: file:///private/var/mobile/Containers/Data/PluginKitPlugin/E82D4A01-A2ED-42E0-9378-420F5B1DBAD3/tmp/.com.apple.Foundation.NSItemProvider.5FKOVj/image

and the result of FileManager.default.attributesOfItem(atPath: url.path) is below:

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]

FYI, this is where I open share extension, after capturing screen. enter image description here

Justin Sato
  • 523
  • 1
  • 4
  • 20
  • Thanks to this question, finally I got the image. https://stackoverflow.com/questions/44498932/ios-share-extension-crashes-when-sharing-from-ios-11-screenshot – Justin Sato Oct 29 '17 at 04:24

1 Answers1

2

i think i have a solution for you.

the screenshot editor provides in the completion handler of the loaditem the image as UIImage

url as? UIImage

Please have a look here: ios swift share-extension: what are all and the best ways to handle images?

Hilmar Demant
  • 515
  • 5
  • 20
  • Thank you @Hilmar. Your answer is perfect. it's very strange to get the image one time, and the url for other time. – Justin Sato Nov 13 '17 at 07:09
  • Hi, yes - i am also wondering (even as there is a 3rd way in form of raw data in addition). Probably as there is no typed API currently. Sounds like a missing feature or bug. – Hilmar Demant Nov 13 '17 at 07:32