Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:55:26+00:00 2026-05-15T18:55:26+00:00

I was previously downloading images for my app by using dataWithContentsOfURL to download a

  • 0

I was previously downloading images for my app by using dataWithContentsOfURL to download a jpg then writeToFile to save it.

I recent;y started using an NSURLConnetion to do the same, but now I am getting the follwoing errors and a crash:

Corrupt JPEG data: 87 extraneous bytes
JPEG datastream contains no image

I know these images are not corrumpt, as the app was downloading them fine using the previous method. Here is my code:

-(void) downloadSave {

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSString *tempString = [[NSString alloc]initWithFormat:@"http://www.mysite.com/%@.jpg",chartFileName];
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:tempString]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:10.0];
    // create the connection with the request
    // and start loading the data

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        mutableData = [[NSMutableData data] retain];
        self.image = nil;
        NSLog(@"connection exists");
        [NSURLConnection connectionWithRequest:theRequest delegate:self];

    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"There was an error contacting the chart servers. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [activityIndicator stopAnimating];


    }




//  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
//                                                       NSUserDomainMask, YES);

//  NSString *docsPath = [paths objectAtIndex:0];
//  NSString *downloadPath = [[[NSString alloc]initWithFormat:@"http://www.mysite.com/%@.jpg",chartFileName]autorelease];
//  downloadedChartData = nil;


    [pool drain];
    [pool release];




}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    NSLog(@"got to connection did receive response");
    [mutableData setLength:0];
}




- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [mutableData appendData:data];
//  NSLog(@"got some data, total: %i",mutableData.length);
}


- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
 //   [connection release];
    // receivedData is declared as a method instance elsewhere
   // self.mutableData = nil;

    // inform the user
    //NSLog(@"Connection failed! Error - %@ %@",
      //    [error localizedDescription],
        //  [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[mutableData length]);
    [connection release];
    // release the connection, and the data object
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    self.image = nil;

    NSString *savePath = [[[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath, chartFileName]autorelease];

    [mutableData writeToFile:savePath atomically:YES];
    self.mutableData = nil;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:55:26+00:00Added an answer on May 15, 2026 at 6:55 pm

    You are initializing and starting two NSURLConnections with the same delegate. As your delegate methods do not check which connection called them you are mixing up the bytes of two times your image in one NSMutableData instance.

    // Creates, initializes and starts an instance of NSURLConnection    
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    ...
    // Creates, initializes and starts another instance of NSURLConnection, with same request and delegate
    [NSURLConnection connectionWithRequest:theRequest delegate:self];
    

    Both connections message the same implementations on the same delegate instance, which means their data is written into the same NSMutableData in random order.

    I would suggest to simply get rid of the line:

    [NSURLConnection connectionWithRequest:theRequest delegate:self];
    

    Another thing: why are you using an autorelease pool in downloadSave? If you call it from the main Thread you have only one NSURLRequest autoreleased in that pool. If you call it from an other Thread you have to take care, that the runloop of that thread is setup and running, or you wouldn’t receive any delegate callbacks at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Previously, I have an app that uses core data. I use same store url
I’m downloading maps using Static Maps V2 API then display map on my mobile
I currently use FixedThreadPool to download images from the web, like this: ExecutorService mThreadPool
This is related to my previous post Problem with downloading multiple files using AsyncTask
I have a thread downloading data and I want to wait until the download
I want to download the 'pyflix' library that was previously hosted at http://svn.pyflix.python-hosting.com/trunk/ .
I am using System.Net.WebClient.DownloadFile to download a large number of html files from a
I am downloading an xml file from ftp and using the following code to
NOTE: This same error also occurs when using easy_install, other ways of installation... I'm
I would like to use Android's system cache when downloading images as per these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.