简体   繁体   English

iOS共享扩展名未获取图像

[英]iOS share extension not getting the image

I am sure this is trivial once someone kindly point me in the right direction so my apology for asking a silly question. 我敢肯定,一旦有人将我指向正确的方向,这对我来说是微不足道的,所以我道歉询问一个愚蠢的问题。 However I have been searching for days I can't figure out what I am doing wrong. 但是我一直在寻找无法弄清楚自己在做什么错的日子。

Scenario: create a simple share extension that receives an image file 方案:创建一个简单的共享扩展名以接收图像文件

Problem: when I access the attachements, the handler is never called albeit I can see the "public.jpg" in the itemProvider but I can't see where the data would be? 问题:当我访问附件时,尽管我可以在itemProvider中看到“ public.jpg”,但从未调用该处理程序,但是却看不到数据在哪里?

What I have done: 我做了什么:

1) defined NSExtensionActivationSupportsImageWithMaxCount = 1 as my only activation rule
2) added CoreMedia framework to the extension
3) added the same group to both app and app extension
4) made sure both have the group (1) in the entitlement
5) made sure both are using a certificate/app id with that group enabled
6) clean and rebuild several times to no avail.

The code: 编码:

- (void)didSelectPost {
/
for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
        if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
I can hit this breakpoint --> [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
but not this one --> photo = image;
            }];
            break;
        }
    }
}
.... and so on and so forth

You haven't posted your complete code but I suspect that you are calling completeRequestReturningItems:completionHandler: at the wrong location: 您尚未发布完整的代码,但我怀疑您在错误的位置调用了completeRequestReturningItems:completionHandler: ::

WRONG : 错误

- (void)didSelectPost {
        NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
        NSItemProvider *itemProvider = item.attachments.firstObject;
        if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
            [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
                                            options:nil
                                  completionHandler:^(NSURL *url, NSError *error) {
                                      // send the image
                                  }];
        }
        // ↓ this is the wrong location ↓
        [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
    }

The problem is that calling completeRequestReturningItems:completionHandler: immediately dismisses the ShareViewController and deallocates it. 问题在于,调用completeRequestReturningItems:completionHandler:立即关闭ShareViewController并对其进行分配。 So the NSItemProvider that contains the image is also destroyed before it can access the image (because it loads its items asynchronously). 因此,包含图像的NSItemProvider在访问图像之前也会被销毁(因为它异步加载其项目)。 In other words: the completion handler in which you send the image to your server is never called, because the whole shareViewController has already been deallocated. 换句话说:永远不会调用将图像发送到服务器的完成处理程序,因为整个shareViewController已经被释放。

To fix that problem you have to move the call to completeRequestReturningItems:completionHandler: to the end of the completion handler AFTER you send the image. 要解决该问题,您必须在发送图像之后将对completeRequestReturningItems:completionHandler:的调用移至完成处理程序的末尾。

CORRECT: 正确:

- (void)didSelectPost {
    NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
    NSItemProvider *itemProvider = item.attachments.firstObject;
    if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
        [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
                                        options:nil
                              completionHandler:^(NSURL *url, NSError *error) {
                                  // send the image
                                  [self.extensionContext completeRequestReturningItems:@[]         
                                                                     completionHandler:nil];
                              }];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM