7

I'm getting an image over HTTP, using NSURLConnection, as follows -

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

Usually it works just fine, but sometimes I'm seeing this get logged - : Corrupt JPEG data: premature end of data segment

At this point, the image does not completely render. I'll see maybe 75% of it, and then the lower right hand corner is a grey box.

Any ideas on how to approach fixing this? Am I constructing my image improperly?

bpapa
  • 21,409
  • 25
  • 99
  • 147
  • I have been downloading many images and haven't seen this yet. Is you image extremely large? Does this happen on other devices (computer, simulator)? – Corey Floyd Jun 30 '09 at 17:56
  • It's not particularly large, no. And I do see it on both the iPhone and the simulator (but not by hitting the image via a web browser). – bpapa Jun 30 '09 at 18:22
  • Please check your internet connections as well. – Abhishek Bedi May 08 '13 at 11:37

5 Answers5

9

Your HTTP code looks correct. You might want to log the size of the receivedData once it's done loading and compare it to the expected size of the image on the server. If it's the expected size, then maybe the image itself is corrupted on the server.

Marc Novakowski
  • 44,628
  • 11
  • 58
  • 63
  • 7
    Thanks, this helped out a lot. By doing this I realized it was my own programming error (I was accidently firing off the request twice). – bpapa Jul 02 '09 at 12:01
  • 1
    I was doing the same thing as bpapa. It seems if you launch 2 different NSURLConnections to the same URL, the end result will be corrupt data. – CornPuff Jul 29 '11 at 21:27
  • 1
    I got the exact same problem, and it was indeed from the same cause: 2 connections to the same image. – Enzo Tran Jan 12 '12 at 12:53
  • 1
    The error is created because both connections are sharing the same data source. When building applications where specific tasks have to fire concurrently (which you did inadvertently, it seems :) ), you either have to have a unique data source for each connection (One-to-One), or create a semaphore so your mutable data source isn't being serialized with the results of multiple connections at once (race conditions). The end result is the creation of corrupted, or incomplete, data! (Which you experienced). – user298261 Aug 17 '12 at 15:05
  • @bpapa - where exactly in your code were you firing off the request twice? Based on my log output, I think I am doing the same, but don't understand why only certain photos would be getting called twice. – Scott Lieberman Feb 26 '13 at 21:15
  • I don't have access to this code anymore, but I believe it was just me making some weird mistakes as an inexperienced iOS developer back in '09. :) – bpapa Feb 27 '13 at 03:26
3

ASI-HTTP can fix this problem.

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

My queue variable in appDelegate is ASINetwork queue object. Because I send asynchronous request, so I use it.

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

I fixed this problem by using an NSMutableDictionary.

NSMutableDictionary *dataDictionary;

In my loadData function, I define my data:

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

Then I load the data into my dictionary where the key is [theConnection description] and the object is my data.

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

That way in the delegates, I can look up the correct data object for the connection that is passed to the delegate and save to the right data instance otherwise I end up with the JPEG munging/corruption problem.

In didReceiveData, I put:

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

Similarly, in didReceiveResponse, I put:

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

And in connectionDidFinishLoading: NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; img = [[UIImage alloc] initWithData:theReceivedData];

And this seems to work very well. By the way, my code is based on Apple's tutorial for NSUrlConnection with the addition of an NSMutableDictionary to keep track of individual connections. I hope this helps. Let me know if you want me to post my full image handling code.

Jackson
  • 3,555
  • 3
  • 34
  • 50
  • I use the same method. Works great. Make sure to alloc and init the data object in the dictionary before you send the request though. Otherwise you will end up with the same mistake – JWKot Jan 13 '14 at 17:34
1

I have seen this also. If you save the data to a file and then read the data back into an image, it works perfectly. I suspect there is http header information in the jpeg image data.

Hope someone finds a solution to this because the save to file workaround sucks.

// problem

UIImage *newImage = [UIImage imageWithData:receivedData];

// crappy workaround

[receivedData writeToFile:[NSString stringWithFormat:@"a.jpg"] atomically:NO];
UIImage *newImage = [UIImage imageWithContentsOfFile:@"a.jpg"];
Tim
  • 59,527
  • 19
  • 156
  • 165
0

Copying the NSData content using receivedData = [NSData dataWithBytes:receivedData.bytes length:receivedData.length] may be helpful too (and it's more efficient than saving to and reading from the disk).

A possible reason for this is that the original receivedData object does not retain its content (e.g. when created using [NSData dataWithBytesNoCopy:length:]) and you try to read them after they are freed.

This is likely when you encounter this problem on another thread from the thread that created the NSData object.

Yoav
  • 5,962
  • 5
  • 39
  • 61