-2

I want to take a UIImage from one class (pulled from a server) and use it in a separate class (different View Controller).

UIImage *image = [UIImage imageWithData:imageData];

This is my current code in the SecondViewController. How can I get it so that I can use the UIImage image in my FirstViewController? Thanks!

Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31

2 Answers2

0

Its very simple... Try NSUserDefaults concept here. You can get UIImage in any view controller of the app if once stored in NSUserDefaults.

Step 1: For Storing

Image Save on NSUserDefaults in second view controller

  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"image"]; 

Step 2: For Retrieving

Image retrieve on NSUserDefaults in first view controller:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
UIImage* image = [UIImage imageWithData:imageData];
Community
  • 1
  • 1
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
0

If you are using .xib to navigate from one ViewController to another then this code might help you.

Suppose you have image in you FirstViewController

UIImage *image = [UIImage imageWithData:imageData];

//------Time to go at another Class lets call it secondViewController

secondViewController *secondview =[[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil WithImage:image];
[self.navigationController pushViewController:secondview animated:YES];

SecondViewController.h

@interface secondViewController : UIViewController

@property (nonatomic,strong)UIImage *imagefromServer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil WithImage:(UIImage *)image;

@end

SecondViewController.m

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil WithImage:(UIImage *)image
  {
       self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
       if (self) {
       // Custom initialization
       imagefromServer= image;
       }
   return self;
  }

Hope helps you

Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31