0

I'm importing an image from the Mail app to my own app, I get the image and try to send it to my main view controller through AppDelegate. I can use NSLog@ to confirm the image is going through, but when I try to use it it shows as null. Here's my code:

In AppDelegate.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {


    if (url !=nil && [url isFileURL]) {
        MainViewController *view = [[MainViewController alloc] init];
        [view handleDocumentOpenURL:url];
        NSLog(@"Url 1: %@", url); // Url appears
    }

    return YES;

}

In my MainViewController.m

- (void)handleDocumentOpenURL:(NSURL *)url {

    NSData *imageData = [NSData dataWithContentsOfURL:url];
    self.importedImage = [UIImage imageWithData:imageData];
    NSLog(@"Image %@", self.importedImage); // Shows the object exists

}

The problem is that if I try to access this UIImage (self.importedImage) from other methods it says that it is null (it works within the above mentioned method).

Additional info: I declare the importedImage as a UIImage on my MainViewController.h file.

I assume my AppDelegate might not be accessing the actual MainViewController but a copy of it. I've looked around for an answer and couldn't find anything, any help is appreciated.

AndreVitorio
  • 622
  • 1
  • 7
  • 18

2 Answers2

0

Please try below code may be its helped

if importedImage is UIView then

[self.importedImage setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithData:imageData]]];

if importedImage is UIImageView then

self.importedImage.image = [UIImage imageWithData:imageData];
ios_dev
  • 1,025
  • 15
  • 27
  • Thanks for you reply. Actually importedImage is an UIImage. Sorry for not mentioning this on the post. – AndreVitorio May 12 '16 at 10:13
  • have you tried after UIImage alloc and init. self.importedImage = [[UIImage alloc]init]; self.importedImage = [UIImage imageWithData:imageData]; – ios_dev May 12 '16 at 12:11
  • I tried that before but it didn't work. I managed to get it right, you can check the answer below. Thanks for you help @Tluck – AndreVitorio May 12 '16 at 12:31
0

I got it! My suspicion that AppDelegate was not grabbing the right ViewController was correct. Here is what I did, in AppDelegate:

 UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MainViewController *result;

//check to see if navbar "get" worked
if (navigationController.viewControllers)

    //look for the nav controller in tab bar views
    for (UINavigationController *view in navigationController.viewControllers) {

        //when found, do the same thing to find the MainController under the nav controller
        if ([view isKindOfClass:[UINavigationController class]])
            for (UIViewController *view2 in view.viewControllers)
                if ([view2 isKindOfClass:[MainViewController class]])
                    result = (MainViewController *) view2;
    }


if (url !=nil && [url isFileURL]) {
    [result handleDocumentOpenURL:url];
    NSLog(@"Url 1: %@", url);
}

I found the answer on this thread: https://stackoverflow.com/a/8791991/4187621

The problem was not the same but the solution worked for me. I hope it helps others.

Community
  • 1
  • 1
AndreVitorio
  • 622
  • 1
  • 7
  • 18