我使用以下代码从 Internet 下载了一个 .jpg 文件:
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
一切正常。我只是不知道如何处理数据。假设我在 IB 中创建了一个图像视图,我将如何显示图像?这是最后一点:
- (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];
}