0

Possible Duplicate:
iOS download and save image inside app

I want to download a file. I found this

[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]

My questions are:

  1. Where does the file saved?

  2. By using threads, is it any possible to build a status bar for downloads?

  3. Are there any way to change the memory(internal/external) to save the file?

Now I am using

NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]]; downloadStatus.text =@"size: %zd", malloc_size(dataImage);

The result is always 32. Shouldn't that be the size of the actual image?

Community
  • 1
  • 1
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • how come you couldn't find any documentation ...http://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app – spider1983 Dec 13 '12 at 06:03

5 Answers5

1
  1. The file is not saved. It's loaded/retrieved and converted into an NSData object.
  2. Yes. However, if you're doing this you should look at NSURLConnection and particularly the NSURLConnectionDataDelegate protocol. You'll need to asynchronously download the file and get the callbacks into the delegate to be able to update your status bar. Or you could use a 3rd party networking library to simplify the whole thing, but it's good to understand what's going on behind the scenes.
  3. Yes. You can save the NSData object as a file when it's downloaded. If you were using Cocoa (not iOS) you could use NSURLDownload to download the file directly.
rickerbh
  • 9,731
  • 1
  • 31
  • 35
0
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:myProgressIndicator];
[request startSynchronous];
NSLog(@"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);

And you can also follow this link to make more underastanding :) http://allseeing-i.com/ASIHTTPRequest/How-to-use

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
  • I recommend using `AFNetworking` instead of ASI libraries, because they're no longer supported. – amb Dec 13 '12 at 07:50
0

You can assign the downloaded data to a variable and show it in an UIImageView as follows.

NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://lasp.colorado.edu/home/wp-content/uploads/2011/03/suncombo1.jpg"]];
self.imageViewYours.image = [UIImage imageWithData:dataImage];
Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
0

Try This Code Worked for Me. The Image will save to your PhotoAlbum.

   -(IBAction)save_Image:(id)sender
    {
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your URL"]]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

            [self performSelectorOnMainThread:@selector(imageDownloaded) withObject:nil waitUntilDone:NO ];

    }

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error != NULL)
    {
        // handle error
    }
    else
    {
        // handle ok status
    }
}

- (void)imageDownloaded
{

      // network animation off
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;



    // do whatever you need to do after
}
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
0

Since you're new to Obj-C, I'd first get familiar with NSURLConnection and NSURLConnectionDataDelegate (protocol). Once you feel comfortable that you know what's going on, you can easily switch to a networking library such as AFNetworking to simplify things.

JaredH
  • 2,338
  • 1
  • 30
  • 40