【问题标题】:iOS 8 share extension show shared image issueiOS 8 共享扩展显示共享图像问题
【发布时间】:2015-04-13 23:26:34
【问题描述】:

我在 UIImageView 中显示图像时遇到问题。

这就是我在 ShareViewController 中获取图像的方式:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    for (NSItemProvider* itemProvider in ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments )
    {
        if([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
        {
            ++counter;

            [itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:
             ^(id<NSSecureCoding> item, NSError *error)
             {
                 UIImage *sharedImage = nil;

                 if([(NSObject*)item isKindOfClass:[NSURL class]])
                 {
                     sharedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:(NSURL*)item]];
                 }
                 if([(NSObject*)item isKindOfClass:[UIImage class]])
                 {
                     sharedImage = (UIImage*)item;
                 }
                 if ([(NSObject *)item isKindOfClass:[NSData class]])
                 {
                     sharedImage = [UIImage imageWithData:(NSData *)item];
                 }

                 [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotificationDidLoadItem" object:sharedImage];
             }];
        }
    }
}

这就是我在 DetailsViewController 中接收通知的方式:

- (void)didLoadSharedImage:(NSNotification *)notification {

    UIImage *sharedImage = [notification object];
    [self.sharedImages addObject:sharedImage];

    if (!self.theImageView.image) {

        self.theImageView.image = sharedImage;
    }
}

所以这个方法之后没有可见的图像。我调试了它,我看到了那个图像,但是当我从 DetailsViewController 推送新的视图控制器然后弹回来时,我看到它很奇怪。只有这样 UIImageView 才像是刷新了自己。

【问题讨论】:

  • 你能告诉我们更多细节吗?
  • @MustafaIbrahim,我首先有两个 UIViewController,它们是根控制器,并且具有扩展上下文。从第一个控制器,我得到了上面代码中的图像。获得图像后,我通过通知将其发送到第二个视图控制器以及上面带有接收器的代码。但图像出现延迟或有时不出现。或者在我从第二个视图控制器进行推送然后返回之后,图像视图会神奇地自我更新
  • 第二个ViewController在你发布通知时是否加载到内存中?
  • @MustafaIbrahim 当然,我什至可以跟踪 - (void)didLoadSharedImage:(NSNotification *)notification 并调用它。所以在调试器中我看到了我设置的图像。
  • 您是否尝试将 didLoadSharedImage 执行到 UIThread 中。比如 dispatch_async(dispatch_get_main_queue(), ^{ if (!self.theImageView.image) { self.theImageView.image = sharedImage; } });

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


【解决方案1】:

您是否尝试在 UIThread 中执行 didLoadSharedImage。喜欢

dispatch_async(dispatch_get_main_queue(), ^{ 
          if (!self.theImageView.image) {
          self.theImageView.image = sharedImage; 
          } 
})

祝你好运

【讨论】:

  • -loadItemForTypeIdentifier:options:completionHandler: 没有在主线程上调用,您需要分派到主线程上。见here。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
相关资源
最近更新 更多