【发布时间】:2011-06-28 19:38:22
【问题描述】:
当我异步使用NSURLConnection 来尝试获取带有我的图像数据的NSData 时,图像返回为空白,但是当我同步使用dataWithContentsOfURL 时,我没有任何问题,并且我正确获取了图像数据.我的异步方法会失败有什么原因吗?
这行得通:
NSData *data = [NSData dataWithContentsOfURL: url];
NSLog(@"TEST %@", data);
UIImage *map = [UIImage imageWithData:data];
mapView.image = map;
这不是:
//
// MapHttpRequest.m
// GTWeb
//
// Created by Graphic Technologies on 6/21/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MapHttpRequest.h"
@implementation MapHttpRequest
@synthesize receivedData;
@synthesize dataString;
@synthesize vc;
- (void)request:(NSString *)url fromView:(UIViewController *) theVC
{
vc = theVC;
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSLog(@"URL: %@", url);
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
[vc mapImageConnectionFinished:receivedData];
// release the connection, and the data object
[dataString release];
[connection release];
[receivedData release];
}
@end
【问题讨论】:
-
查看使用
NSURLConnection的代码会有所帮助。 -
@highlycaffeinated 我已经编辑了它,我不小心按了 Enter,它在我准备好之前就发布了。
-
我看不到你在哪里填充你创建的 receivedData 对象。
-
我确实发现返回的 MIME 类型是 text/html,即使 url 清楚地以 .jpg 结尾。 NSURLConnection 不是将其视为文本是否有某些原因?
-
我将 receivedData 转换为 String 并打印出来,它是:
Bad request
。当我的浏览器和 dataWithContentsOfUrl 都没有问题使用相同的 url 返回图像时,为什么 NSURLConnection 会返回错误的请求?
标签: ios iphone asynchronous nsurlconnection