iOS的份额8扩展显示共享图像问题(iOS 8 share extension show share

2019-10-22 15:02发布

我有我的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好像刷新自身。

Answer 1:

你有没有尝试执行didLoadSharedImage到UIThread。 喜欢

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

祝好运



文章来源: iOS 8 share extension show shared image issue