我已经创建了一个类,用于在基于NSURLConnection的iOS-App中获取服务器端数据,将获取的数据转发给委托进行处理。xcode NSURLConnection NSMutableData appendData仅适用于某些类型的内容
当下载文本/ JSON这个完美的作品。
我现在正在扩展获取一些PNG图像,无法获取数据。
我发现它在didReceiveData-call中断,其中[receivedData appendData:data]对接收到的图像数据不做任何处理。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
[receivedData appendData:data];
}
我得到数据到这个方法(NSData的> 0字节),但调用[receivedData appendData:数据]不改变receivedData(静像0字节)的大小。
由于这与文本完美配合,而不是与图像数据相关,我认为它与字符集或编码有关,但无法在其上找到任何内容。
任何帮助表示赞赏。
更新与请求的代码样本:
在使呼叫的方法:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [NSMutableData data];
// Also tried with no change in behaviour
// receivedData = [[NSMutableData alloc] initWithLength:0];
} else {
// Inform the user that the connection failed.
}
我didReceiveResponse
:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
另一个更新
原来,问题BOIS下来并发我火类的三个实例同时放至asynchroniously取三个文件。问题是第一个版本receivedData
在完成破坏来自其他两个实例的响应时。
如何确保每个课程实例都有自己的receivedData
玩?
最后说明 使用MutableArrays为retreived数据的词典解决并发问题。
它也好像定义MutableArray作为类的私有属性为每个实例创建一个数组。
@interface MyClass(){
@private NSMutableData * receivedData;
}
我想'receivedData'只是'nil'。 – omz
Plz向我们展示您的应用程序中的“ - (void)连接:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应 ”方法。 –
'连接didReceiveResponse'只需调用'[receivedData setLength:0]' – mariusnn