tags:

views:

310

answers:

2

I downloaded a .jpg file from the Internet with this code:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cvcl.mit.edu/hybrid/cat2.jpg"]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
receivedData=[[NSMutableData data] retain]; // etc etc

Everything works fine. I just don't know how to handle the data. Say I created an image view in IB, how would I go about displaying the image? Here's the last bit:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // ******do something with the data*********...but how  
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}
+1  A: 

UIImage has a method +imageWithData:

mjhoy
And if you need this on the Mac, use NSImage's initWithData: method.
Dave DeLong
aye.. whoops, was it a Mac question? I've been programming too long :)
mjhoy
There's no indication of whether it's a Mac or iPhone question.
Peter Hosey
+2  A: 

Assuming your NSImageView is attached as an IBOutlet called imageView you should be able to initialize an image using the received data and then set the image view to display the image.

NSImage *image = [[NSImage alloc] initWithData:receivedData];
[imageView setImage:image];
[image release];
Olly