title: XCODE: How to save button image after change?
body: <p>UPDATED----</p>
  
  <p>Ok, so I've found a way to do it....may not be the right way, but I'm new to xcode and this way seems to have worked for me. Maybe someone out there can improve on the idea.</p>
  
  <p>The original question was how to save a button image after it has been changed? Ok, well here goes....I have a custom button set up with an image, so the first thing I did was change the image for that button using this code: (btw it is set up so that you click one button to change the image of the custom button)</p>
  
  <pre><code>.h file
  
  @interface viewcontroller{
  
  IBOutlet UIButton *btnSetImage1;
  
  }
  
  @property (nonatomic, retain) IBOutlet UIButton *btnSetImage1;
  </code></pre>
  
  <p>Then the code:</p>
  
  <pre><code>.m file
  
  @synthesize btnSetImage1;
  
  //Inside a button's action
  
  UIImage *btnImage1 = [UIImage imageNamed:@"button_green.png"];
          [btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal]; 
  </code></pre>
  
  <p>-----------------------HERE'S WHERE THE QUESTION GETS ANSWERED------------------------------</p>
  
  <p>Ok, so now that the image is changed on the custom button, I saved it by adding this:</p>
  
  <pre><code>    .h file
  
  @interface viewcontroller{
  
  IBOutlet UIButton *btnSetImage1;
  IBOutlet UIImageView *buttonImgChange1;
  
  }
  
  @property (nonatomic, retain) IBOutlet UIButton *btnSetImage1;
  @property (nonatomic, retain) IBOutlet UIImageView *buttonImgChange1;
  </code></pre>
  
  <p>Then the code:</p>
  
  <pre><code>.m file
  
  @synthesize btnSetImage1;
  @synthesize buttonImgChange1;
  
  //Inside a button's action
  
  UIImage *btnImage1 = [UIImage imageNamed:@"button_green.png"];
          [btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal]; 
  
  //Set image to variable  
  
  buttonImgChange1 = [[UIImageView alloc] initWithImage:btnImage1];
  
          //SAVE CHANGES
  
          // Create instances of NSData
          UIImage *savedButtonImage1 = buttonImgChange1.image;
          NSData *buttonData1 = UIImagePNGRepresentation(savedButtonImage1);    
          // Store the data
          NSUserDefaults *buttondefaults1 = [NSUserDefaults standardUserDefaults];
          [buttondefaults1 setObject:buttonData1 forKey:@"buttonImage1"];
  
          [buttondefaults1 synchronize];
  </code></pre>
  
  <p>With that I was able to save the button image as a key called "buttonImage1". Then I just called for this key in the viewdidload:</p>
  
  <pre><code>//-----------UPDATE BUTTONS---------------
  
  
  // Get the stored data before the view loads
  NSUserDefaults *buttondefaults1 = [NSUserDefaults standardUserDefaults];
  
  NSData *buttonData1 = [buttondefaults1 dataForKey:@"buttonImage1"];
  UIImage *savedButtonImage1 = [UIImage imageWithData:buttonData1];
  
  
  // Update the UI elements with the saved data
  buttonImgChange1.image = savedButtonImage1;
  
  
  //Update buttons
  UIImage *btnImage1 = savedButtonImage1;
  [btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal];
  
  buttonImgChange1 = [[UIImageView alloc] initWithImage:btnImage1];
  </code></pre>
  
  <p>Like I said...I'm new to xcode and looked all over for a solution to this question. After fumbling through it myself I came up with this solution. It may not be the best or most efficient, I don't know, but it worked for me and I hope it helps someone else along the way.</p>
  
  <p>Oh, and just a note, if you want two different images, one that shows in the buttons normal state and then a different one when the button is clicked, simply copy all the code and rename all the variables, the forKey name, and finally change the forState to - forState:UIControlStateHighlighted. And there you have it, a button that when pressed changes and saves a custom button's image.</p>
  
  <p>Thanks!</p>