iOS 8 share extension show shared image issue

ⅰ亾dé卋堺 提交于 2019-12-11 10:41:22

问题


I have a problem with displaying image in my UIImageView.

So this is how I get images in 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];
             }];
        }
    }
}

this is how I receive notification in DetailsViewController:

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

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

    if (!self.theImageView.image) {

        self.theImageView.image = sharedImage;
    }
}

So there is no visible image after this method. I debugged it and I saw that image is there, but it's strange that I see it when I push new view controller form DetailsViewController and then pop back. Only then UIImageView seems like refresh its self.


回答1:


Did you try to execute didLoadSharedImage into UIThread. like

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

Good luck



来源:https://stackoverflow.com/questions/29616701/ios-8-share-extension-show-shared-image-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!