2009-06-30 38 views
7

我收到了HTTP的图像,使用NSURLConnection的好评,如下 -iPhone - 图像JPEG损坏数据通过HTTP

NSMutableData *receivedData; 

- (void)getImage { 
    self.receivedData = [[NSMutableData alloc] init]; 
    NSURLConnection *theConnection = // create connection 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
    [receivedData appendData:data]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 

    UIImage *theImage = [UIImage imageWithData:receivedData]; 
} 

通常它工作得很好,但有时我看到这个记录到日志中 - :损坏的JPEG数据:数据段过早结束

此时,图像不完全呈现。我会看到它的75%,然后右下角是一个灰色的框。

有关如何解决此问题的任何想法?我构图不当吗?

+0

我一直在下载很多图片,但还没有看到。 你的形象非常大吗?这是否发生在其他设备(电脑,模拟器)? – 2009-06-30 17:56:06

+0

这不是特别大,没有。我确实在iPhone和模拟器上看到了它(但不是通过网络浏览器击中图像)。 – bpapa 2009-06-30 18:22:14

+0

请检查您的互联网连接。 – 2013-05-08 11:37:03

回答

9

您的HTTP代码看起来正确。您可能想要记录receivedData完成加载后的大小,并将其与服务器上图像的预期大小进行比较。如果这是预期的大小,那么图像本身可能在服务器上损坏。

+7

谢谢,这有助于很多。通过这样做,我意识到这是我自己的编程错误(我意外地发出请求两次)。 – bpapa 2009-07-02 12:01:33

1

我也见过这个。如果您将数据保存到文件中,然后将数据读回到图像中,则可以完美地工作。我怀疑jpeg图像数据中有http头信息。

希望有人找到解决办法,因为保存到文件的解决方法很糟糕。

// problem 

UIImage *newImage = [UIImage imageWithData:receivedData]; 

// crappy workaround 

[receivedData writeToFile:[NSString stringWithFormat:@"a.jpg"] atomically:NO]; 
UIImage *newImage = [UIImage imageWithContentsOfFile:@"a.jpg"];
3

ASI-HTTP可以解决这个问题。

NSURL *coverRequestUrl = [NSURL URLWithString:imageStringURL]; 
ASIHTTPRequest *coverRequest = [[ASIHTTPRequest alloc] initWithURL:coverRequestUrl]; 
[coverRequest setDelegate:self]; 
[coverRequest setDidFinishSelector:@selector(imageRecieved:)]; 

[appDelegate.queue addOperation:coverRequest]; 
[appDelegate.queue go]; 

我的appDelegate中的队列变量是ASINetwork队列对象。因为我发送异步请求,所以我使用它。

- (void)imageRecieved:(ASIHTTPRequest *)response 
{ 
    UIImage *myImage = [UIImage imageWithData:[response responseData]]; 
} 
2

我通过使用NSMutableDictionary解决了这个问题。

NSMutableDictionary *dataDictionary; 

以我loadData功能,我定义我的数据:

NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init]; 

然后我将数据加载到我的字典,其中关键字为[theConnection描述]和对象是我的数据。

[dataDictionary setObject:receivedData forKey:[theConnection description]]; 

在代表这样的话,我可以查找正确的数据对象传递给委托的连接,并保存到正确的数据实例,否则我结束与JPEG改写(munging)/腐败问题。

在didReceiveData,我把:

//get the object for the connection that has been passed to connectionDidRecieveData and that object will be the data variable for that instance of the connection. 
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 

//then act on that data 
[theReceivedData appendData:data]; 

类似地,在didReceiveResponse,我把:

NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 
[theReceivedData setLength:0]; 

而在connectionDidFinishLoading: NSMutableData * theReceivedData = [数据字典objectForKey:[连接描述]] ; img = [[UIImage alloc] initWithData:theReceivedData];

这似乎工作得很好。顺便说一下,我的代码基于Apple's tutorial for NSUrlConnection,并增加了一个NSMutableDictionary来跟踪各个连接。我希望这有帮助。让我知道你是否希望我发布我的完整图像处理代码。

0

使用receivedData = [NSData dataWithBytes:receivedData.bytes length:receivedData.length]复制NSData内容也可能有帮助(并且比保存和读取磁盘更有效)。

可能的原因是原始的receivedData对象没有保留其内容(例如,当使用[NSData dataWithBytesNoCopy:length:]创建时),并且在释放它们后尝试读取它们。

这很可能是您在创建NSData对象的线程的另一个线程上遇到此问题的原因。