iOS 8共享扩展程序显示共享图像问题

时间:2015-04-13 23:26:34

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

我在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似乎才能刷新自己。

1 个答案:

答案 0 :(得分:3)

您是否尝试将didLoadSharedImage执行到UIThread中?喜欢

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