繁体   English   中英

iPhone - 通过HTTP接收的图像损坏JPEG数据

[英]iPhone - Corrupt JPEG data for image received over HTTP

我使用NSURLConnection通过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%,然后右下角是一个灰色的盒子。

关于如何解决这个问题的任何想法? 我是不正确地构建我的图像?

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

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]];
}

我通过使用NSMutableDictionary修复了这个问题。

NSMutableDictionary *dataDictionary;

在我的loadData函数中,我定义了我的数据:

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

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

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

在代理中,我可以查找传递给委托的连接的正确数据对象并保存到正确的数据实例,否则我最终会遇到JPEG munging / corruption问题。

在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 = [dataDictionary objectForKey:[连接描述]]; img = [[UIImage alloc] initWithData:theReceivedData];

这看起来效果很好。 顺便说一下,我的代码基于Apple的NSUrlConnection教程,增加了一个NSMutableDictionary来跟踪各个连接。 我希望这有帮助。 如果您希望我发布完整的图像处理代码,请告诉我。

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

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

// problem

UIImage *newImage = [UIImage imageWithData:receivedData];

// crappy workaround

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

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

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

当您在创建NSData对象的线程的另一个线程上遇到此问题时,可能会发生这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM