title: memory leaks issue while saving images to directory
body: <p>Please try to understand my question.
  i am picking images from phone library and saving into Documents Directory. But When I pick large number of images the utilised memory increases gradually and reach above of 400 mb then my app crash. Please if anybody can solve my problem what should I do? I'm new comer to Objective C. Any response will be appreciated.
  here is my code </p>
  
  <p>when Picker finish picking</p>
  
  <pre><code>- (void)agImagePickerController:(AGImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info  {
  [self ShowLoadingView:@"Files Are Loading...."];
  [self performSelectorInBackground:@selector(saveAllSelectedImages:) withObject:info];}
  </code></pre>
  
  <p>and then I save images to Directory</p>
  
  <pre><code>-(void) saveAllSelectedImages:(NSArray*)imagesArray{
  
  for (int i=0; i&lt;imagesArray.count; i++) {
  
      ALAsset *asset = [imagesArray objectAtIndex:i];
      ALAssetRepresentation *alassetRep = [asset defaultRepresentation];
      NSDate *currentDate = [NSDate date];
      NSString* DucPath = [[AppDelegate GetDocumentDirectoryPath] stringByAppendingPathComponent:@"Media"];
      if (![[NSFileManager defaultManager] fileExistsAtPath:DucPath])
          [[NSFileManager defaultManager] createDirectoryAtPath:DucPath withIntermediateDirectories:NO attributes:nil error:nil];
  
      if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
      {
  
  
  
          long long DataSize = [alassetRep size];
          Byte *buffer = (Byte*)malloc(DataSize);
          NSUInteger buffered = (NSUInteger)[alassetRep getBytes:buffer fromOffset:0.0 length:alassetRep.size error:nil];
          NSData *videoData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
  
          NSString* newVideoName = [NSString stringWithFormat:@"video_%d_%d.mov",(int)currentDate,i];
          NSString* newVideoPath = [DucPath stringByAppendingPathComponent:newVideoName];
          [videoData writeToFile:newVideoPath atomically:YES];
  
          [pImageMediaArray addObject:newVideoName];
      }
      else
      {
          UIImage *image = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
  
          /************************************Full Resolution Images ******************************************/
  
          NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
          image = nil;
  
          NSString *originalPath = [NSString stringWithFormat:@"IMAGE_%d_%d.jpg",(int)currentDate,i];
          NSString* pImagePath = [DucPath stringByAppendingPathComponent:originalPath];
  
          [imageData writeToFile:pImagePath atomically:YES];
  
          [pImageMediaArray addObject:originalPath];
  
      }
      /************************************Low Resolution Images ******************************************/
      UIImage *image = [UIImage imageWithCGImage:[alassetRep fullResolutionImage]];
      UIImage *thumbImage = [self imageWithImage:image scaledToSize:CGSizeMake(50, 50)];
  
      NSData *thumbImageData = UIImageJPEGRepresentation(thumbImage, 0.8);
  
  
      NSString *thumbOriginalPath = [NSString stringWithFormat:@"SMALL_IMAGE_%d_%d.jpg",(int)currentDate,i];
      NSString* thumbImagePath = [DucPath stringByAppendingPathComponent:thumbOriginalPath];
      NSLog(@"Image path At Save Time:%@",thumbImagePath);
      [thumbImageData writeToFile:thumbImagePath atomically:YES];
  
      [pMediaArray addObject:thumbOriginalPath];
  }
  
  [appDelegate setPMediaArray:pImageMediaArray];
  
  
  [pGridView reloadData];
  imagesArray = nil;
  [imagesArray release];
  [pImageMediaArray release];
  [self performSelectorOnMainThread:@selector(closeLoadindView) withObject:nil waitUntilDone:YES];}
  </code></pre>