title: iOS: Download image from url and save in device
body: <p>I am trying to download the image from the url <a href="http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg" rel="noreferrer">http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg</a></p>
  
  <p>I am using the following code, but image is not saved in the device. I want to know what I am doing wrong.</p>
  
  <pre><code> NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];
   [NSURLConnection connectionWithRequest:request delegate:self];
  
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];
   NSData *thedata = NULL;
   [thedata writeToFile:localFilePath atomically:YES];
  
   UIImage *img = [[UIImage alloc] initWithData:thedata];
  </code></pre>