title: iOS: Converting An Image To PNG To Allow For Saving
body: <p>The app that I'm working on allows the user to pick an image from their camera roll, and display it in a UIImageView. Now, I'm having some issues trying to save the data. I know that I must convert the image to PNG and save the data, but I'm having some issues doing that. I'm very new to file saving and Objective-C in general. Here is the code I'm using:</p>
  
  <pre><code>- (NSString *)dataFilePath {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                           NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      return [documentsDirectory stringByAppendingPathComponent:kFilename9];
  }
  
  - (void)applicationDidEnterBackground:(NSNotification *)notification {
      NSMutableArray *array = [[NSMutableArray alloc] init];
  
      NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)];
      [array addObject:imageData];
  
      [array writeToFile:[self dataFilePath] atomically:YES];
  }
  - (void)viewDidLoad
  {
      self.imgPicker = [[UIImagePickerController alloc] init];
      self.imgPicker.allowsImageEditing = YES;
      self.imgPicker.delegate = self;
      self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  
  
      NSString *filePath = [self dataFilePath];
      if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
          NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
          image.image = [array objectAtIndex:0];
      }
  
      UIApplication *app = [UIApplication sharedApplication];
      [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(applicationDidEnterBackground:)
                                                   name:UIApplicationDidEnterBackgroundNotification
                                                 object:app];
  
      [super viewDidLoad];
  
  }
  </code></pre>
  
  <p>The program crashed upon simply tapping the tab that this takes place in. The debugger states "<strong>'NSInvalidArgumentException', reason: '-[__NSCFData _isResizable]: unrecognized selector sent to instance 0x16cf50'</strong>". Any help is much appreciated, thank you!</p>