View Full Version : Saving Images on Phone

Aug 5, 2008, 03:14 PM

I use of a bunch of images from the web in my app. I get them using URL requests and store them in NSData structures. It would be nice, however, if I could save the downloaded images on the iPhone for when the program loads again. I'm unfamiliar with saving and where it would save to. In essence I would like my program to check if an Image is already downloaded, and if it's not, then it could do so and save it

any thoughts?


Aug 5, 2008, 03:59 PM
Use NSSearchPathForDirectoriesInDomains() along with NSDocumentDirectory to get the Documents folder on the iPhone for your application. Then you can append the image's name, and use NSData's writeToFile:atomically: method to write the data to the file.

Aug 6, 2008, 01:14 PM
works perfectly. thanks.

Aug 7, 2008, 01:21 AM
Hey guys,

I have attempted the method that KainJow suggested in the reply. But somehow i wasn't able to get the expected result. Instead i got these errors in my debugger console. Can someone please tell me what wrong and what should i do with it?


Debugger Console


2008-08-07 14:12:18.303 BoringHelloWorld[10821:20b] Succeeded! Received 4247 bytes of data
2008-08-07 14:12:18.402 BoringHelloWorld[10821:20b]
--> Info: Saving...

[Session started at 2008-08-07 14:12:18 +0800.]
2008-08-07 14:12:18.403 BoringHelloWorld[10821:20b]
--> Info: /Users/intern/Library/Application Support/iPhone Simulator/User/Applications/515E64F2-7611-4D05-A78B-CDC8376B1064/Documents

Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-960) (Sun May 18 18:38:33 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).
Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/intern/Library/Application Support/iPhone Simulator/User/Applications/515E64F2-7611-4D05-A78B-CDC8376B1064/BoringHelloWorld.app/BoringHelloWorld', process 10821.
error while killing target (killing anyway): warning: error on line 1980 of "/SourceCache/gdb/gdb-960/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)

The Debugger has exited with status 0.(gdb)

Aug 7, 2008, 08:30 AM
Post some code please. That debugger text is generic.

Aug 7, 2008, 08:26 PM

Here are part of the codes that I have implemented to search for document directory and URL testing to download and save images into the iphone.


- (NSString*) documentsDirectory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex: 0];
[self logError: @"Documents directory not found!"];
[self info: documentsDirectory];
return documentsDirectory;

- (BOOL) writeApplicationData: (NSData *)data toDirectory: (NSString *)directory toFile: (NSString *)fileName
NSString *appFile = [directory stringByAppendingPathComponent: fileName];
[self showAlertWithTitle: @"File Path" message: appFile];
//return ([data writeToFile: appFile atomically: YES]);
return ([data writeToFile: appFile atomically: NO]);

- (void) URLTest
NSString *url = @"http://earthquake.usgs.gov/images/globes/-5_130.jpg";
NSURLRequest* theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: url] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];
NSURLConnection* theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self];
// Create the NSMutableData that will hold the received data
// receivedData is declared as a method instance elsewhere
receivedData = [[NSMutableData data] retain];
[self showAlertWithTitle: @"URL Test" message: @"Download could be made"];
// inform the user that the download could not be made
[self showAlertWithTitle: @"URL Test" message: @"Download could not be made"];

- (void) connectionDidFinishLoading: (NSURLConnection*)connection
// do something with the data
NSString *string = [[NSString alloc] initWithData: receivedData encoding: NSASCIIStringEncoding];
NSLog(@"\n\nData: \n\n%@", string);
[self showAlertWithTitle: @"receiveData is nil" message: nil];
BOOL result = NO;
[self info: @"Saving..."];
NSString *appFile = [[self documentsDirectory] stringByAppendingPathComponent: @"savedImageData"];
result = [receivedData writeToFile: appFile atomically: YES]; // Error here
[self showAlertWithTitle: @"Saving" message: @"Successful"];
[self showAlertWithTitle: @"Saving" message: @"Failed"];
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);
// release the connection, and the data object
[connection release];
[receivedData release];
[self showAlertWithTitle: @"URL Test" message: @"Did finish loading"];