title: UIImage gets corrupted while Downloading
body: <p>I have developed iOS App, in which i am downloading image from server and saving it in my Document directory.</p>
  
  <p>But problem which i am facing is, sometimes my images getting corrupted when i download, even if the server response isSuccessful.</p>
  
  <p>Here is my code snippet,</p>
  
  <p><strong>urlFile is path of UIImage which is on server e.g: www.abcd.com/images/pqr.jpg</strong></p>
  
  <p><strong>fileName which i am using for saving image name in my DocDirectory.</strong></p>
  
  <pre><code>- (void)downloadFile:(NSString *)urlFile withName:(NSString *)fileName 
  {
      NSString *trimmedString = [urlFile stringByTrimmingCharactersInSet:
                                 [NSCharacterSet whitespaceAndNewlineCharacterSet]];
  
      NSLog(@"trimmedString=%@",trimmedString);
      if ([trimmedString length]&gt;0)
      {
          HTTPEaterResponse *response = [HTTPEater get:[NSString stringWithFormat:@"%@",trimmedString]];
  
      if ([response isSuccessful])
          {  
             NSLog(@"isSuccessful");
              [self saveImage:[[UIImage alloc] initWithData:[response body]] withName:fileName];
          } else {
              NSLog(@"Url response failed %@", [response description]);
          }
      }
  
  }
  
  
      - (void)saveImage:(UIImage *)image withName:(NSString *)name
      {
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
  
          NSData *data = UIImagePNGRepresentation(image);
          NSLog(@"image =%@",image);
  
          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
          [fileManager createFileAtPath:fullPath contents:data attributes:nil];
  
      }
  </code></pre>
  
  <p>When i see my Log, it shows:</p>
  
  <ul>
  <li>trimmedString= www.abcd.com/images/pqr.jpg</li>
  <li>isSuccessful</li>
  <li>image =null</li>
  </ul>
  
  <p><strong>Thanks in advance.</strong></p>