最后我找到了我的问题的答案......我在这里粘贴以供将来参考
声明一个全局变量
NSMutableData *receivedData;
在 ViewDidLoad 中:
receivedData=[[NSMutableData alloc]init];
NSURLRequest *theRequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wallpaperswide.com/download/flattened_grass-wallpaper-2800x1050.jpg"]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
}
else
{
// Inform the user that the connection failed.
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[connectFailMessage show];
}
NSURLConnection 委托方法..
#pragma mark NSURLConnection methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
//inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *tmpImage = [[UIImage alloc] initWithData:receivedData];
myImageView.image=tmpImage;
NSLog(@"%@",tmpImage);
}