initWithContentsOfURL
initWithContentsOfURL
- Subject: initWithContentsOfURL
- From: Gary C Martin <email@hidden>
- Date: Wed, 8 Aug 2001 15:14:44 +0100
As another newcomer to Cocoa here, I've been bouncing about the
documentation trying to get the the initWithContentsOfURL method for
NSImage to actually return something other than nil objects.
Apologies if I'm being particularly thick but below is a selector method
I thought should work to initialize (NSImage *)myImage from a NSURL
ready for use, however it always returns as a nil object:
- (void)URLResourceDidFinishLoading:(NSURL *)sender
{
// this always returns nil :o(
NSImage *myImage = [[[NSImage alloc] initWithContentsOfURL:sender]
autorelease];
// do other things with myImage here
}
Now I have managed to get the job done with an alternative route that
works just fine (see below). It's just that I'd like to get my head
around why the above initWithContentsOfURL code fails. Here's a working
method:
- (void)URLResourceDidFinishLoading:(NSURL *)sender
{
// correctly returns a valid NSImage
NSImage *myImage = [[[NSImage alloc] initWith
Data:[sender
resourceDataUsingCache:YES]] autorelease];
// do other things with myImage here
}
Oooh, here's the method that kicks off the initial loading attempt:
- (void)loadImageWithURL: (id)sender
{
// sample uk weather map URL
NSURL *myURL = [NSURL
URLWithString:@"
http://eur.yimg.com/w/eu/english/outlook/uk24c.jpg"];
[myURL loadResourceDataNotifyingClient:self usingCache:YES];
}
Thanks for any guiding words.
Regards,
Gary C Martin