Transferring UIImage from one class to another

<>

This question already has an answer here:

Answers


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];

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


Need Your Help

Do access modifiers affect reflection also?

.net reflection access-modifiers

I always believe they did, but seeing some answers here make me doubt...