2009-06-30 73 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對象的線程的另一個線程上遇到此問題的原因。