简体   繁体   中英

iOS 8 share extension show shared image issue

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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