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

bpa*_*apa 7 iphone cocoa-touch uiimage

我使用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];
}
Run Code Online (Sandbox Code Playgroud)

通常它工作正常,但有时我看到这会被记录 - :损坏的JPEG数据:数据段的过早结束

此时,图像无法完全呈现.我会看到它的75%,然后右下角是一个灰色的盒子.

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

Mar*_*ski 9

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

  • 谢谢,这帮了很多忙.通过这样做,我意识到这是我自己的编程错误(我意外地两次触发请求). (7认同)