NSImage initWithContentsOfURL
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");
}