iPhone - Corrupt JPEG data for image received over HTTP

后端 未结 5 1712
我寻月下人不归
我寻月下人不归 2020-12-10 16:04

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

NSMutableData *receivedData;

- (void)getImage {
    self.receivedData = [[NSMutableDat         


        
相关标签:
5条回答
  • 2020-12-10 16:38

    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.

    0 讨论(0)
  • 2020-12-10 16:55

    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.

    0 讨论(0)
  • 2020-12-10 16:56

    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"];
    0 讨论(0)
  • 2020-12-10 17:00

    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]];
    }
    
    0 讨论(0)
  • 2020-12-10 17:00

    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.

    0 讨论(0)
提交回复
热议问题