Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 4th, 2013  |  syntax: None  |  size: 0.76 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NSURL *url = [NSURL URLWithString:@"http://example.com/image.jpg"];
  2. NSData *data = [NSData dataWithContentsOfURL:url];
  3. UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
  4.        
  5. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  6. {
  7.     if (requestData)
  8.     {
  9.         self.image = [[[UIImage alloc] initWithData:requestData] autorelease];
  10.     }
  11. }
  12.        
  13. [self performSelectorInBackground:@selector(loadImage) withObject:nil];
  14.  
  15. - (void)loadImage
  16. {
  17.    NSURL * url = [NSURL URLWithString:@"http://.../....jpg"];
  18.    NSData * data = [NSData dataWithContentsOfURL:url];
  19.    UIImage * image = [UIImage imageWithData:data];
  20.    if (image)
  21.    {
  22.        // Success use the image
  23.        ...
  24.    }
  25.    else
  26.    {
  27.        // Failed (load an error image?)
  28.        ...
  29.    }
  30. }