2017-10-28 18 views
1

앱 확장 프로그램에서 이미지를 가져 오려고합니다. 문제는 형식이 "public.image"일 때 이미지를 가져올 수 없다는 것입니다. 그러나 형식이 "public.png"이면 성공합니다. 내가하고 싶은 것은 단지 이미지를 가져 와서 서버에 업로드하는 것입니다.공유 확장을 통해 이미지를 열거 나 읽을 수 없습니다.

다음은 내가 사용하고있는 코드입니다. I는 loadFileRepresentation() 방법에서 URL을 검사

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이 실패하는 경우와 같다 ​​: 는 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] 

FYI이있다 화면 캡처 후 공유 확장을 엽니 다. enter image description here

+0

감사, 마침내는 이미지를 가지고 :

스크린 샷 편집기가있는 UIImage

url as? UIImage 

로 loaditem의 완성 처리기에서 여기 봐주세요 이미지를 제공합니다. https://stackoverflow.com/questions/44498932/ios-share-extension-crashes-when-sharing-from-ios-11-screenshot –

답변

1

나는 당신에게 해결책이 있다고 생각합니다. 이 질문에 ios swift share-extension: what are all and the best ways to handle images?

+0

감사합니다 @Hilmar. 당신의 대답은 완벽합니다. 이미지를 한 번 가져 오는 것은 매우 이상하고 다른 시간은 URL을 가져 오는 것이 좋습니다. –

+0

안녕하세요, 네 - 저는 또한 궁금합니다 (원시 데이터 형식의 세 번째 방법이 추가로 있음에도 불구하고). 현재 입력 된 API가 없기 때문일 수 있습니다. 누락 된 기능이나 버그가있는 것 같습니다. –