Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

attodorov

macrumors newbie
Original poster
Apr 17, 2008
5
0
Hi guys,

I am using the latest iphone SDK (Beta 3). I am getting an image from an URL, and then trying to save it via the NSData's writeToFile function to a file in the app's Documents folder. It crashes with EXC_BAD_ACCESS.

Do you have any idea why ? Thanks very much.

The code is the following:

==================== myappAppDelegate.m ==========

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];

NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];

NSString *filename = @"savedImage.jpg";

NSString *url = @"http://www.promovid.co.nz/images/ms_logo.jpg";

NSData *imageData;
NSURL *imageURL = [[NSURL alloc] initWithString:url];
if (imageURL) {
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:filename];

NSLog(appFile);

[imageData writeToFile:appFile atomically:YES];
}


=============================================
 

Littleodie914

macrumors 68000
Jun 9, 2004
1,813
8
Rochester, NY
Well, first, you're re-declaring your imageData pointer.

Have you used the debugger before? That might help you figure out which line of code is causing the problem.

If you haven't, try putting a bunch of NSLog()'s in your code to see where it's stopping. Example:

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

NSLog(@"1"); // First change

NSString *documentsDirectory = [paths objectAtIndex:0];

NSLog(@"2"); // Second change

if (!documentsDirectory) {
NSLog(@"Documents directory not found!"); 
} 

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:filename];
NSLog(@"3"); // Third change
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Well, first, you're re-declaring your imageData pointer.

And that is the problem. There are two variables named imageData: The first one is never initialised, and that is the one that you try to write to the file. The second one exists only for the shortest possible amount of time; the data from the URL is assigned to it, then the variable is thrown away.

I think the compiler should have given a warning about that.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
it is in a different scope. If it were in the same scope it would be a redeclaration error.

That's why it should have been a warning. It is completely legal Objective C, so it is not an error, but any programmer who spots it just _knows_ that this cannot possibly be what the author meant. That's what compilers give warnings for; things that are not illegal but likely mistakes.

There should also be a warning because the original variable was used uninitialised.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.