Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 27th, 2013  |  syntax: None  |  size: 2.17 KB  |  hits: 30  |  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. UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
  2.        
  3. - (void)downloadWorker:(NSString *)urlString
  4. {
  5.     NSAutoreleasePool *pool  = [[NSAutoreleasePool alloc] init];
  6.     NSURL             *url   = [NSURL URLWithString:urlString];
  7.     NSData            *data  = [NSData dataWithContentsOfURL:url];
  8.     UIImage           *image = [[UIImage alloc] initWithData:data];
  9.  
  10.     [self performSelectorOnMainThread:@selector(imageLoaded:)
  11.                            withObject:image
  12.                         waitUntilDone:YES];
  13.     [image release];
  14.     [pool drain];
  15. }
  16.  
  17. - (void)downloadImageOnThread:(NSString *)url
  18. {
  19.     [NSThread detachNewThreadSelector:@selector(downloadWorker:)
  20.                              toTarget:self
  21.                            withObject:url];
  22. }
  23.  
  24. - (void)imageLoaded:(UIImage *)image
  25. {
  26.     // get the image into the UI
  27. }
  28.        
  29. - (void)downloadAvatar:(NSString *)URL{
  30.  NSURL *url = [[NSURL alloc] initWithString:URL];
  31.  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
  32.  [url release];
  33.  NSURLDownload *download = [[NSURLDownload alloc] initWithRequest:request delegate:self];
  34.  NSString *path = [[NSString alloc] initWithFormat:@"%@data/%@.jpg",[[BFAppSupport defaultSupport] bfFolderPath],[[xfSession loginIdentity] userName]];
  35.  [download setDestination:path allowOverwrite:YES];
  36.  [download release];
  37.  [path release];
  38.  [request release];
  39. }
  40. - (void)downloadDidFinish:(NSURLDownload *)download{
  41.  NSString *path = [[NSString alloc] initWithFormat:@"%@data/%@.jpg",[[BFAppSupport   defaultSupport] bfFolderPath],[[xfSession loginIdentity] userName]];
  42.  NSData *imageData = [[NSData alloc] initWithContentsOfFile:path];
  43. if( [imageData length] < 10 ){
  44.   [self performSelector:@selector(downloadAvatar:) withObject:@"http://media.xfire.com/xfire/xf/images/avatars/gallery/default/xfire160.jpg" afterDelay:0.0];
  45.   [imageData release];
  46.   [path release];
  47.   return;
  48. }
  49. NSImage *theImage = [[NSImage alloc] initWithData:imageData];
  50. [imageData release];
  51. [path release];
  52. [yourImage setImage:theImage];
  53. [theImage release];
  54. }
  55.  
  56. - (void)download:(NSURLDownload *)aDownload didFailWithError:(NSError *)error{
  57.  NSLog(@"Avatar url download failed");
  58. }