• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSImage initWithContentsOfURL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSImage initWithContentsOfURL


  • Subject: NSImage initWithContentsOfURL
  • From: John Nikolai <email@hidden>
  • Date: Wed, 23 Jan 2002 10:56:42 -0800

Hello everyone!

I have been working through Aaron Hillegass's excellent book "Cocoa Programming for Mac OS X" (yep, I'm a newbie to Objective-C) and ran into a problem while trying something new.

Basicly I want to download an image from a given URL. NSImage offers a message called "initWithContentOfURL: NSURL" that seemed like a good starting point. So I coded something like this:

----------
NSURL *url = [NSURL URLWithString:@"http://www.google.com/images/logo.gif";];
image = [[NSImage alloc] initWithContentsOfURL: url];
----------

I verified that the image does exist on the site and that I have access to it via IE. Unfortunately this code does not work and my image returned is nil.

So I thought maybe I needed to download the image first before sending the "initWithContentsOfURL" message. So I implemented the NSURLHandleClient protocol and added the following code below -- but the callbacks are never called!

What am I missing?

Thanks for all your help,
- John

-----------------
NSURL *url = [NSURL URLWithString:@"http://www.google.com/images/logo.gif";];
[url loadResourceDataNotifyingClient:self usingCache:false];

// Callback messages
- (void)URLHandleResourceDidBeginLoading:(NSURLHandle *)sender
{
NSLog(@"1");
}

- (void)URLHandleResourceDidCancelLoading:(NSURLHandle *)sender
{
NSLog(@"2");
}

- (void)URLHandleResourceDidFinishLoading:(NSURLHandle *)sender
{
NSLog(@"3");
}

- (void)URLHandle:(NSURLHandle *)sender resourceDataDidBecomeAvailable:(NSData *)newBytes
{
NSLog(@"4");
}

- (void)URLHandle:(NSURLHandle *)sender resourceDidFailLoadingWithReason:(NSString *)reason
{
NSLog(@"5");
}


  • Follow-Ups:
    • Re: NSImage initWithContentsOfURL
      • From: Matt Judy <email@hidden>
  • Prev by Date: Re: NSString drawInRect clipping control sorted
  • Next by Date: NSTabView without tabs
  • Previous by thread: Re: NSTableView edit text-field help please?
  • Next by thread: Re: NSImage initWithContentsOfURL
  • Index(es):
    • Date
    • Thread