1

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.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Can you get us more details Plz? – Mustafa Ibrahim Apr 14 '15 at 00:02
  • @MustafaIbrahim, I have two UIViewControllers first which is root controller and which has extension context. From first controller I get images like in the code above. After I get an image I send it via notification to second view controller and the code with receiver also above. But the image appears with delay or sometimes does not appear. Or after I make push from second view controller and then go back, image view magically update itself – Matrosov Oleksandr Apr 14 '15 at 00:05
  • Does the secondViewController is loaded in memory when you post the notification? – Mustafa Ibrahim Apr 14 '15 at 00:08
  • @MustafaIbrahim sure, I even can track - (void)didLoadSharedImage:(NSNotification *)notification and it invokes. so in debugger I see image which I set. – Matrosov Oleksandr Apr 14 '15 at 00:14
  • 1
    Did you try to execute didLoadSharedImage into UIThread. like dispatch_async(dispatch_get_main_queue(), ^{ if (!self.theImageView.image) { self.theImageView.image = sharedImage; } }); – Mustafa Ibrahim Apr 14 '15 at 00:16
  • @MustafaIbrahim great thanks it works!!! please add it as an answer – Matrosov Oleksandr Apr 14 '15 at 00:30
  • @MustafaIbrahim how did understand that?) – Matrosov Oleksandr Apr 14 '15 at 00:31
  • You say that the debugging is show the image correctly. So I guessed that it's a multithreading problem. the best practise is to ensure that you execute your notification code into UIThread. – Mustafa Ibrahim Apr 14 '15 at 00:35

1 Answers1

3

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

Mustafa Ibrahim
  • 1,110
  • 1
  • 9
  • 17
  • `-loadItemForTypeIdentifier:options:completionHandler:` isn't called on the main thread, you need to dispatch onto the main thread. See [here](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSItemProvider_Class/index.html#//apple_ref/occ/instm/NSItemProvider/loadItemForTypeIdentifier:options:completionHandler:). – Tim Johnsen Apr 14 '15 at 02:38